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:11:08
 
 
To
28/06/2012 18:08:06
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2008
Miscellaneous
Thread ID:
01547167
Message ID:
01547171
Views:
26
>>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.

Thanks Bruce. Off to read up and I'll report back.


Charles Hankey

Though a good deal is too strange to be believed, nothing is too strange to have happened.
- Thomas Hardy

Half the harm that is done in this world is due to people who want to feel important. They don't mean to do harm-- but the harm does not interest them. Or they do not see it, or they justify it because they are absorbed in the endless struggle to think well of themselves.

-- T. S. Eliot
Democracy is two wolves and a sheep voting on what to have for lunch.
Liberty is a well-armed sheep contesting the vote.
- Ben Franklin

Pardon him, Theodotus. He is a barbarian, and thinks that the customs of his tribe and island are the laws of nature.
Previous
Reply
Map
View

Click here to load this message in the networking platform