rows into columns

Hi there,
I have a query that returns two rows per each license. What I need to do is to combine those two records into one.
the data looks like this:
AddressType       ATTN        Line1                     UNIT CITY                STATE     POSTALCODE    Country
Mailing Address ATTNTO      1234 SW 21 AVE      12      TOWNNAME      STATE      12345                  US
Site Address           ATTNTO      3456 SW 31 AVE            TOWNNAME      STATE     12345                  US

and I need the data like this:
Mailing AddressATTN       Mailing Address Line1    Mailing AddressUNIT  Mailing AddressCITY  Mailing AddressSTATE     Mailing AddressPOSTALCODE    Mailing AddressCountry  SiteAddressATTNTO SiteAddressTOWNNAME      SiteAddressSTATE ...
How can I do this?

here is the original query :
SELECT ADDRESSTYPE, ATTN, 
CASE WHEN ADDRESSTYPE = 'Mailing Address' then ADDREsSLINE1 else 
ADDRESSLINE1 + ' ' + isnull(PREDIRECTION,'') + ' ' + isnull(ADDRESSLINE2, '') + ' ' + isnull(STREETTYPE, '') END as Line1,
CASE WHEN UNITORSUITE <> '' then 'UNIT/SUITE: ' + UNITORSUITE else '' end as Unit
, CITY, [STATE], POSTALCODE, COUNTRY 
FROM BLAddresses

Open in new window

COHFLAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
How do you know 2 address lines belong together?

EG:  If you have more than 2 rows, which combination go together?


Essentially you could do a self join:
select ma.AddressLine1, sa.AddressLine1 from 
Bladdresses ma
left join  Bladdresses sa on ma.CustomerID = sa.CustomerID and sa.AddressType ='SiteAddress'   --for demo only, use the key
where ma.AddressType = 'Mailing Address'

Open in new window

COHFLAuthor Commented:
Good question there is a field that I didn't include it was LicenseNumber
Kyle AbrahamsSenior .Net DeveloperCommented:
So join on licensenumber . . . note that I changed the join to a full join so that you get all records:


select ma.*, sa.*
from 
Bladdresses ma
full join  Bladdresses sa on ma.LicenseNumber = sa.LicenseNumber and sa.AddressType ='SiteAddress' 
where ma.AddressType = 'Mailing Address'

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.