sql動態(tài)行轉(zhuǎn)列的兩種方法
第一種方法:
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)
- mysql 行轉(zhuǎn)列和列轉(zhuǎn)行實例詳解
- sql語句實現(xiàn)行轉(zhuǎn)列的3種方法實例
- SQLServer行轉(zhuǎn)列實現(xiàn)思路記錄
- MySQL存儲過程中使用動態(tài)行轉(zhuǎn)列
- mssql 數(shù)據(jù)庫表行轉(zhuǎn)列,列轉(zhuǎn)行終極方案
- Sql Server 2000 行轉(zhuǎn)列的實現(xiàn)(橫排)
- SQL查詢語句行轉(zhuǎn)列橫向顯示實例解析
- table 行轉(zhuǎn)列的sql詳解
- SQL行轉(zhuǎn)列和列轉(zhuǎn)行代碼詳解
- SQL基礎(chǔ)教程之行轉(zhuǎn)列Pivot函數(shù)
相關(guān)文章
SQL Server將一列的多行內(nèi)容拼接成一行的實現(xiàn)方法
這篇文章主要介紹了SQL Server將一列的多行內(nèi)容拼接成一行的實現(xiàn)方法,需要的朋友可以參考下2015-11-11如何強制刪除或恢復(fù)SQLServer正在使用的數(shù)據(jù)庫
如何強制刪除或恢復(fù)SQLServer正在使用的數(shù)據(jù)庫...2007-01-01使用 TOP 子句限制UPDATE 語句更新的數(shù)據(jù)
這篇文章主要介紹了使用 TOP 子句限制UPDATE 語句更新的數(shù)據(jù),需要的朋友可以參考下2014-08-08SQL?Server創(chuàng)建用戶定義函數(shù)
這篇文章介紹了SQL?Server創(chuàng)建用戶定義函數(shù)的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05SQL中concat、concat_ws()、group_concat()的使用與區(qū)別
本文主要介紹了SQL中concat、concat_ws()、group_concat()的使用與區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05SQL中的單條件判斷函數(shù)IF和多條件判斷CASE WHEN的用法
MySQL提供了IF、IFNULL、CASE等條件判斷函數(shù),本文就來介紹一下SQL中的單條件判斷函數(shù)IF和多條件判斷CASE WHEN的用法,感興趣的可以了解一下2023-10-10Access 數(shù)據(jù)類型與 MS SQL 數(shù)據(jù)類型的相應(yīng)
Access 數(shù)據(jù)類型與 MS SQL 數(shù)據(jù)類型的相應(yīng)...2006-10-10