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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Concatenating multiple rows and columns?

Author  Topic 

phrankbooth
Posting Yak Master

162 Posts

Posted - 2014-12-12 : 16:10:56
Hi,

I'd like to convert the following:

Select 'Obj1' Field1, 'Some Value 1,' Value, NULL Product union all
Select 'Obj1' Field1, 'Some Value 2,' Value, NULL Product union all
Select 'Obj1' Field1, 'Some Value 3,' Value, 'My Product 1 ' Product union all

Select 'Obj2' Field1, 'Some Value 4,' Value, NULL Product union all
Select 'Obj2' Field1, 'Some Value 5,' Value, NULL Product union all
Select 'Obj2' Field1, 'Some Value 6,' Value, 'My Product 2 ' Product

Into these two single field rows:
'Some Value 1, Some Value 2, Some Value 3, My Product 1'
'Some Value 4, Some Value 5, Some Value 6, My Product 2'

Tried this:
SELECT Field1+ ' ' + case when Product IS null then '' else Product end as [text()]
FROM #TMP
FOR XML PATH ('')

But just gives me a single field with everything, I need one row field per grouping. I'd prefer not using the XML pattern, but it's the closest I could get.

Thanks!

--PhB

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-13 : 09:10:01
add a group by clause on product
Go to Top of Page
   

- Advertisement -