I am looking for a SQL query to get sql row data to a column.
SQL Table Data
CustomerId Name Type FoodName
============= ============= ========= ==========
C0001 Thomas BREAKFAST Milk
C0001 Thomas BREAKFAST Bread
C0001 Thomas BREAKFAST Egg
C0001 Thomas LUNCH Rice
C0001 Thomas LUNCH Fish Curry
C0001 Thomas LUNCH Lessy
C0002 JOSEPH BREAKFAST Bread
C0002 JOSEPH BREAKFAST Jam
C0002 JOSEPH BREAKFAST Tea
The above is the SQL Table Data and I need this data in following format. Can anybody help me to create a SQL Qyery to get below output ?
CustomerId Name BREAKFAST LUNCH
============ ============= ================ ==========
C0001 Thomas Milk, Bread, Egg Rice, Fish Curry, Lessy
C0002 JOSEPH Tea,Bread, Jam
Please help me to create SQL Query to get the above output.
I Tried the following query
with DietList(CustomerId,Name,BREAKFAST, DINNER) as
(
select distinct CustomerId 'CustomerId', Name as 'Name'
,(
select a.foodName + ',' as [text()] from
(select AdmnNo,foodName from DietCard) as a
where c.CustomerId = a.CustomerId and c.DietSchedule = 'BREAKFAST' and convert(varchar, [c.ScheduleDate], 112) = '20180402'
for xml path('')
) 'EVENINGTEA'
from DietCard
)
,(
select a.foodName + ',' as [text()] from
(select AdmnNo,foodName from DietCard) as a
where c.CustomerId = a.CustomerId and c.DietSchedule = 'DINNER' and convert(varchar, [c.ScheduleDate], 112) = '20180402'
for xml path('')
) 'DINNER'
from DietCard
)
select * from DietListList where BREAKFAST is not null or DINNER is not null
Thanks in Advance.