zoukankan      html  css  js  c++  java
  • SQL Server- 行列转换 行转列,多行转多列

     部分内容已转至  http://www.zyxpp.com   欢迎访问 

    效果如图,把同一个 code, 按 cate 列分为 Actual 和 Budget 两行,再把mode 每种类型转换成 列名 ,主要用到了 max 函数,很实用

     if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#t'))
     drop table #t  
       create table #t(
         code varchar(10),
       cname nvarchar(30),
         fyear varchar(30),
         cate varchar(10),
         mt numeric(18,4),
         amt numeric(18,2),
         mode nvarchar(20),
         mo_mt numeric(18,4),
         mo_avgfee numeric(18,2),
         mo_rate nvarchar(20)
      )
    insert into #t 
    select            '400',N'深圳','2017','Actual','280','1400','BLK',10,1,'3.57%'
     union all select '400',N'深圳','2017','Actual','280','1400','V15',20,2,'7.14%'
     union all select '400',N'深圳','2017','Actual','280','1400','V5',30,3,'10.71%'
     union all select '400',N'深圳','2017','Actual','280','1400','V0',40,4,'14.29%'
     union all select '400',N'深圳','2017','Actual','280','1400','V20',50,5,'17.86%'
     union all select '400',N'深圳','2017','Actual','280','1400','V10',60,6,'21.43%'
     union all select '400',N'深圳','2017','Actual','280','1400','V25',70,7,'25.00%'
     union all select '400',N'深圳','2018','Budget','280','0','BLK',10,1,'3.57'
     union all select '400',N'深圳','2018','Budget','280','0','V15',20,2,'7.14%'
     union all select '400',N'深圳','2018','Budget','280','0','V5',30,3,'10.71%'
     union all select '400',N'深圳','2018','Budget','280','0','V0',40,4,'14.29%'
     union all select '400',N'深圳','2018','Budget','280','0','V20',50,5,'17.86%'
     union all select '400',N'深圳','2018','Budget','280','0','V10',60,6,'21.43%'
     union all select '400',N'深圳','2018','Budget','280','0','V25',70,7,'25.00%'
     select * from #t
    
     --增加一个强制mode 排序,比如从 vo v1 v2 依次排序
     if exists(select * from tempdb..sysobjects where id=object_id('tempdb..#sort'))
     drop table #sort  
       create Table #sort  
      (
       mode varchar(10),
      )
      insert into #sort
      select distinct mode FROM #t   GROUP BY mode order by mode
      -- select * from #sort
    
       declare @sql nvarchar(max)     --声明一个变量
     SET @sql = '
    SELECT code ' 
    +',cname '
    +',fyear '
    +',cate ' --+ N'''类别''' 
    +',isnull(mt,0) mt' -- + N'''吨数''' 
    +',isnull(amt,0) amt'  -- + N'''金额''' 
    select @sql = @sql + ' , max(case mode when ''' + mode+ ''' then mo_rate  else '''' end) [' + mode+ ']' 
                + ' , max(case mode when ''' + mode+ ''' then mo_avgfee  else 0 end) [' + mode+ '_unit]'
    from (select  mode FROM #sort ) as a 
    --print @sql 
    set @sql = @sql + ' from #t group by code, cname,fyear,cate,mt,amt  order by code,fyear  ' 
    print @sql   
    exec(@sql)   --执行该sql
  • 相关阅读:
    轻轻松松教你写日志-超级简单
    JAVA实现KNN分类
    怎样给filter加入自己定义接口
    设计模式之Visitor模式(笔记)
    Leet Code OJ 237. Delete Node in a Linked List [Difficulty: Easy]
    在C#中怎样推断线程当前所处的状态
    leetCode 83.Remove Duplicates from Sorted List(删除排序链表的反复) 解题思路和方法
    谈谈源码管理那点事儿(一)——源码管理十诫(转)
    安装gi的时候回退root.sh的运行
    Android开发学习之路--异步消息Handler,Message,Looper和AsyncTask之初体验
  • 原文地址:https://www.cnblogs.com/cnishop/p/10723587.html
Copyright © 2011-2022 走看看