Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Set based was to concat strings from n children
Message
From
28/06/2012 18:08:06
 
 
To
28/06/2012 17:44:11
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2008
Miscellaneous
Thread ID:
01547167
Message ID:
01547170
Views:
28
>Trying to avoid cursors and procedural code in tsql
>
>I have a table derived from an SSIS feed that has 1 - n rows that are related, based on symbol, eff_date. If there are more than 1, I want to concatenate the values in a varchar column into the first row
>
>1000 rows, may have 200 rows belonging to 60 sets of 2-4 members
>
>First row of set and any row that has no set has a Seq_num = 1 ( also have a column fgkey int which acts as a key for a set so can use that instead of symbol, eff_date to find rows belong to a set. Singleton rows have no fgkey )
>
>I also have a bit column which I want to be 1 in the first row if any of the rows in the set have a 1
>
>So
>symbol eff_date terms price_sens seq_num
>MSFT 20120601 'string1' 0 1
>MSFT 20120601 'string2' 1 2
>MSFT 20120601 'string3' 0 3
>
>would become
>
>MSFT 20120601 'string1 , string2 , string3' 1
>MSFT 20120601 'string2' 1 -- ( this can stay as it is)
>MSFT 20120601 'string3' 0 -- ( this can stay as it is)
>
>Basically it would be an aggregate function but on strings instead of adding numbers.
>
>If a sum of a bit column would work for finding if the total > 0 I can work that part out.
>
>Looking for some set-based command or magic that would concat the strings
>
>TIA

Hi Charles,

I recently had to do something similar (but simpler) for a quick one-off report. Take a look at some of the ideas proposed here

http://stackoverflow.com/questions/194852/concatenate-many-rows-into-a-single-text-string.

The second answer is the one I implemented. I have to admit, I have no idea how it works, but it is very cool.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform