一道sql 關於pivot的面試題

分析:其實它是對時間月份行轉列的設計

表結構設計:blog

結果:table

select Name,moth,ISNULL([01],0) as Value1,ISNULL([02],0) as Value2,ISNULL([03],0) as Value3 from (
select a.Name,CONVERT(varchar(4),moth,120) as moth,CONVERT(varchar(2),moth,100) as moth2,number from Table_Type as a
left join Table_SHUJU as b on a.id=b.id
) as table1 pivot (sum(number) for moth2 in([01],[02],[03])) as table2select

 

效果圖:im

相關文章
相關標籤/搜索