sql中縱表變橫表

縱表格式如圖所示:sql

查詢sql語句以下:spa

Select ID, Time,convert(varchar(8000),content)content,Date 
from SummerChina 
where countryID='86' and LineType='0'

 

變成橫表如圖所示:code

 

縱表變橫表sql語句以下:blog

select Time,
max(case Date when 'Morning' then content else null end) as Morning,
max(case Date when 'Afternoon' then content  else null end)as Afternoon
from (Select ID, Time,convert(varchar(8000),content)content,Date 
from SummerChina 
where countryID='86' and LineType='0') as t
group by Time
相關文章
相關標籤/搜索