国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

sql動態行轉列的兩種方法

2024-08-31 00:56:28
字體:
來源:轉載
供稿:網友

第一種方法:

復制代碼 代碼如下:


select *from ( select Url,case when Month=01 then '1月' when Month=02 then '2月' when Month=03 then '3月' when Month=04 then '4月' when Month=05 then '5月' when Month=06 then '6月' when Month=07 then '7月' when Month=08 then '8月' when Month=09 then '9月' when Month=10 then ' 10月' when Month=11 then '11月' when Month=12 then ' 12月'

end month,Quality from (

select Url,DATENAME(M,AuditingTime)Month,SUM(Quality) Quality from tb_order as a left join tb_WebSiteInfo as b on a.WebSiteInfoID=b.ID left join tb_OrderList as c on c.OrderID=a.ID where AuditingTime>'2013-01-01' and b.ID>0 and Auditing=2

group by Url,DATENAME(M,AuditingTime) )as h ) as hh

pivot ( sum(Quality) for month in([1月],[2月],[3月],[4月],[5月],[6月],[7月],[8月],[9月],[10月],[11月],[12月])) as a


第二種方法:

復制代碼 代碼如下:


declare @sql varchar(8000)

select @sql = isnull(@sql + ',' , '') + '['+CONVERT(varchar(7),AuditingTime,20)+']'

from tb_order as a left join tb_WebSiteInfo as b on a.WebSiteInfoID=b.ID left join tb_OrderList as c on c.OrderID=a.ID where AuditingTime>'2013-01-01' and b.ID>0 and Auditing=2

group by CONVERT(varchar(7),AuditingTime,20) print @sql declare @sql2 varchar(8000)='' set @sql2=' select *from (

select Url, CONVERT(varchar(7),AuditingTime,20) AuditingTime,SUM(Quality) Quality from tb_order as a left join tb_WebSiteInfo as b on a.WebSiteInfoID=b.ID left join tb_OrderList as c on c.OrderID=a.ID where b.ID>0 and Auditing=2

group by Url, CONVERT(varchar(7),AuditingTime,20)

) as hh pivot (sum(Quality) for AuditingTime in (' + @sql + ')) b'

print @sql2

exec(@sql2)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临猗县| 墨脱县| 南陵县| 上蔡县| 洮南市| 安陆市| 中方县| 遵化市| 鹤峰县| 白朗县| 堆龙德庆县| 榆社县| 吉林省| 白银市| 施秉县| 枝江市| 上杭县| 新营市| 绥江县| 长宁县| 北辰区| 丰顺县| 黔西| 福安市| 绵阳市| 乌拉特中旗| 雅安市| 天柱县| 凭祥市| 南丹县| 博兴县| 宁都县| 南投市| 蒙自县| 丹寨县| 玉山县| 兴山县| 醴陵市| 平昌县| 永清县| 乌兰浩特市|