using COALESCE to combine multiple columns into a single column.

I have a very large query that currently contains many subqueries. One of my subqueries returns several rows. I want to take each row and combine it into one column. I also want to grab several columns per row and combine them. So I'm looking for my data to appear in the columns like this. The current query below is what I was going to use if I put the data in its own table, but then I found the command COALESCE and wondered how I could accomplish what I want without creating a new table. ec.OWNER = '' would have an owner from the main query to tie together.


select ec.CAR, ec.MODEL, ec.DATE_ACQUIRED,
from PROD.dbo.EMPCODES ec where TYPE='CE' and EMP_APP = 0 and MODEL in ('TY','FD', 'AU', 'CV')
and ec.OWNER = '')

Open in new window

NickMalloySystems LeadAsked:
Who is Participating?
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.

Shaun KlineLead Software EngineerCommented:
COALESCE is an expanded version of ISNULL. What you describe can be done using the STUFF command with XML, if you are running on a server that is SQL 2005 and greater.
Shaun KlineLead Software EngineerCommented:
Here is a working example of the STUFF command:
declare @Test TABLE
    ID int identity(1,1),
    Field1 varchar(10),
    Field2 varchar(10)

insert into @Test (field1, field2)
values ('AAA', 'Bob')

insert into @Test (field1, field2)
values ('AAA', 'John')

insert into @Test (field1, field2)
values ('AAA', 'Frank')

insert into @Test (field1, field2)
values ('BBB', 'Jan')

insert into @Test (field1, field2)
values ('BBB', 'Cat')

insert into @Test (field1, field2)
values ('BBB', 'Dana')

insert into @Test (field1, field2)
values ('CCC', 'London')

insert into @Test (field1, field2)
values ('CCC', 'New York')

insert into @Test (field1, field2)
values ('CCC', 'Paris')

SELECT field1, STUFF((SELECT ',' + Field2 FROM @Test a where a.field1 = b.field1 ORDER BY Field1 FOR XML PATH('')), 1, 1, '')
from @Test b
group by field1

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

From novice to tech pro — start learning today.