Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Group by result in a string...
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Environment versions
SQL Server:
SQL Server 2008
Application:
Desktop
Miscellaneous
Thread ID:
01462361
Message ID:
01462367
Views:
29
You wanted to concatenate some Field in a string? Which field?
select Sort_Plan_Number, 
stuff(select ',' + MyOtherField as NewField from myTable T where T.Sort_Plan_Number = T1.Sort_Plan_Number and  Valid_Status = -3300 
for XML PATH('')),1,1,'') as NewField from dbo.StagingRollup 
   WHERE Valid_Status = -3300 
 GROUP BY Sort_Plan_Number
See explanation of this technique in these blogs:

http://jahaines.blogspot.com/2009/06/concatenating-column-values-part-1.html
http://jahaines.blogspot.com/2009/07/concatenating-column-values-part-2.html

http://wiki.lessthandot.com/index.php/Concatenate_Values_From_Multiple_Rows_Into_One_Column_Ordered

and my favorite of all

http://bradsruminations.blogspot.com/2009/10/making-list-and-checking-it-twice.html

>But that will give me rows and I still need to fetch the row in a string.
>The result I am locking for is someting like this: '678,567,234'
>---------------
>>>I need to report in one string a list of value from a group by.
>>>Ex:
>>> SELECT Sort_Plan_Number
>>> FROM dbo.StagingRollup
>>> WHERE Valid_Status = -3300
>>> GROUP BY Sort_Plan_Number
>>>
>>>I can use this select in a cursor and then fetch it but I think with SQL 2008 there is a better way.
>>>Anyone know this one?
>>
>>
>>;with cte as (select *, row_number() over (
>>partition by Sort_Plan_Number order by Sort_Plan_Number) as Row
>> from dbo.StagingRollup 
>> WHERE Valid_Status = -3300 )
>>
>>select * from cte where Row = 1
>>
>>Take a look here
>>
>>Including an Aggregated Column's Related Values
>>Including an Aggregated Column's Related Values - Part 2
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform