sql多表行轉列、級聯行轉列示例代碼。本站提示廣大學習愛好者:(sql多表行轉列、級聯行轉列示例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是sql多表行轉列、級聯行轉列示例代碼正文
現有兩表A,B
A表存儲商品點擊日記,B表存儲商品
請求顯示當天一切商品點擊量列表並附帶總數並按天排序
declare @sql varchar(2000)
set @sql='select CONVERT(varchar(100), a.[Time], 23) as 時光,count(b.title) as 總數'
select @sql =@sql+ ',sum(case b.title when '''+Title+''' then 1 else 0 end) as '+'['+Title+']'
from (select distinct title from B) as B
set @sql=@sql+' from A as a left join B as b on a.AId=B.Aid
group by CONVERT(varchar(100), a.[Time], 23) order by CONVERT(varchar(100), a.[Time], 23) desc'
exec(@sql)
顯示成果
時光
總數
商品1
商品2
商品3
2013-11-11
5
1
0
4
2013-11-10
13
2
11
1