現(xiàn)有兩表A,B
A表存儲(chǔ)商品點(diǎn)擊日志,B表存儲(chǔ)商品
要求顯示當(dāng)天所有商品點(diǎn)擊量列表并附帶總數(shù)并按天排序
代碼如下:
declare @sql varchar(2000)
set @sql='select CONVERT(varchar(100), a.[Time], 23) as 時(shí)間,count(b.title) as 總數(shù)'
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)
顯示結(jié)果
更多信息請(qǐng)查看IT技術(shù)專欄