主页 > 知识库 > sql动态行转列的两种方法

sql动态行转列的两种方法

热门标签:铁路电话系统 地方门户网站 呼叫中心市场需求 AI电销 Linux服务器 服务外包 百度竞价排名 网站排名优化

第一种方法:

复制代码 代码如下:

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 行转列和列转行实例详解
  • sql语句实现行转列的3种方法实例
  • SQLServer行转列实现思路记录
  • MySQL存储过程中使用动态行转列
  • mssql 数据库表行转列,列转行终极方案
  • Sql Server 2000 行转列的实现(横排)
  • SQL查询语句行转列横向显示实例解析
  • table 行转列的sql详解
  • SQL行转列和列转行代码详解
  • SQL基础教程之行转列Pivot函数

标签:兰州 崇左 黄山 衡水 湖南 湘潭 仙桃 铜川

巨人网络通讯声明:本文标题《sql动态行转列的两种方法》,本文关键词  ;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 收缩
    • 微信客服
    • 微信二维码
    • 电话咨询

    • 400-1100-266