question

red68 avatar image
red68 asked

Case expression in SQL Server 2012

Sample data: CUST_ID BUS_UNT Category 42500712 Questioner All three 42500712 FUN All three 42500712 ADV All three 42349515 ADV ADV+Ques 42349515 Questioner ADV+Ques In table above, I want to group by cust_id and check bus_unt field. For all records with same cust_id, if they have all 3 bus_unt (Questioner,FUN,ADV), set category to "All Three". If like last 2 records only have bus_unt (ADV & Questioner), set category to ADV+Questioner. Please help if you can! I am playing with Case expression. Thanks!
sql-server-2012concatenation
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
red68 avatar image
red68 answered
I figured it out in case anyone has same issue. SELECT Cust_ID, STUFF( (SELECT Distinct '+' + Bus_Unt FROM Table WHERE Cust_ID = a.Cust_ID FOR XML PATH ('')) , 1, 1, '') AS Bus_Unit_Group FROM Table AS a GROUP BY Cust_ID ORDER BY Cust_ID
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.