Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have three tables, Orders, ShippingAddress & BillingAddress. ShippingAddress has OrderID, FirstName, LastName & ZipCode. BillingAddress also has OrderID, FirstName, LastName & ZipCode. I want to make it one long list where the order ID appears twice rather than make it an inner join where FirstName, LastName & ZipCode appear twice. It should be easy, but I can't think of how to do it.
SELECT sa.[id] ,sa.[FirstName] ,sa.[LastName] ,LEFT([sa].[PostalCode],5) as 'zip'FROM [SC].[CGB].[ShippingAddress ] saINNER JOIN [SC].[CGB].[BillingAddress] ba ON sa.OrderID = ba.OrderIDWhere sa.OrderID IN (SELECT [ID]FROM [SC].[CGB].[Orders]WHERE ORDERSTATUS='1'
-SergioI use Microsoft SQL 2008
James K
Master Smack Fu Yak Hacker
3873 Posts
Posted - 2014-03-10 : 17:18:03
Looks like what you want is UNION ALL (or UNION if you want to avoid duplicates)
select id, firstname, lastname, LEFT([PostalCode],5) as 'zip' from BillingAddressunion allselect id, firstname, lastname, LEFT([PostalCode],5) as 'zip' from ShippingAddress
SergioM
Posting Yak Master
170 Posts
Posted - 2014-03-11 : 11:26:13
heh, that was easy enough. I've never used union all, but it makes sense. Thanks-SergioI use Microsoft SQL 2008