I have a data in this format
Cust_ID Order_Bname
----------------------
1 Samsung
1 Apple
1 pixel
2 Apple
3 pixel
3 LG
I want output in this format:
Cust_ID Order_Bname1 order_Bname2 Order_Bname3
-----------------------------------------------------------
1 Samsung Apple Pixel
2 Apple Null Null
3 Pixel LG null
Basically I want single row for each customer.
I have tried FOR XML PATH
like this
SELECT Cust_ID + ',' + Order_Bname
FROM temp1
FOR XML PATH ('')
This returns output in this format
Cust_ID Order
---------------------------------
1 Apple,Samsung,Pixel
2 apple
3 pixel,LG
Is there a way to achieve this? I am not able to figure out how
Update 2:
WITH CTE_count
as
(
select Customer_ID as ID,Brand_Name +'-'+ cast([Sale_Amount] as nvarchar)
as
brandname,ROW_NUMBER() OVER (PARTITION BY Customer_ID ORDER BY Customer_ID)
AS lineNumber
from dbo.sheet1
group by Customer_ID,Brand_Name,[Sale_Amount]
)
SELECT ID, brandname,lineNumber
FROM CTE_count
using this I have got this format:
Now how do I create column dynamically from row number and put brand name in to them