SQL Server表竖列转横向显示
----------聯(lián)合查詢結(jié)果集存入臨時表
select * into ##tmpa from (select wh_WhName,sum(ps_DiscountMoney) as moneys,psr.wh_Id,Convert(varchar(10),ps_PSRTime,120) as ps_PSRTime
from PosSaleRecord as psr inner join wareHouse as wh on wh.wh_Id=psr.wh_Id group by psr.wh_Id,wh_WhName,Convert(varchar(10),ps_PSRTime,120)) a
?----------把臨時表豎列轉(zhuǎn)換橫向顯示
declare @sql varchar(8000)???????? --將豎列橫向顯示
set @sql = 'select ps_PSRTime '
select @sql = @sql + ' , (case wh_whname when ''' + wh_whname + ''' then moneys else 0 end) [' + wh_whname + ']'
from (
select distinct wh_whname from ##tmpa
) as a
set @sql = @sql + ' into ##tmpPPPt from ##tmpa'
exec(@sql)
----------查詢臨時表
select * from ##tmpa???
select * from ##tmpPPPt
----------刪除臨時表
drop table ##tmpPPPt
drop table ##tmpa
轉(zhuǎn)載于:https://www.cnblogs.com/wuhuisheng/archive/2011/06/02/2068346.html
總結(jié)
以上是生活随笔為你收集整理的SQL Server表竖列转横向显示的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将某字符串切割成阵列并排序列出
- 下一篇: C# 按钮美化技巧