• oracle 行转列 分组分列显示


    原始数据

    type 分 1,2,3,4,5

         select name,yield ,type
          --type将数据分成不同组,为每组生成编号
          ,row_number() over(partition by type order by yield desc) rn
          --,dense_rank() over(order by yield desc) rn 
          from AAA 
    

     

    根据分组生成每组对应的编号

    最终sql

    select 
     max(case when type = 1 then name end) as lv1 , max(decode(type,1,yield)) Ayield,
     max(case when type = 2 then name end) as lv2 , max(decode(type,2,yield)) Byield,
     max(case when type = 3 then name end) as lv3 , max(decode(type,3,yield)) Cyield,
     max(case when type = 4 then name end) as lv4 , max(decode(type,4,yield)) Dyield,
     max(case when type = 5 then name end) as lv5 , max(decode(type,5,yield)) Eyield
    from 
    (
          select name,yield ,type
          --type将数据分成不同组,为每组生成编号
          ,row_number() over(partition by type order by yield desc) rn
          --,dense_rank() over(order by yield desc) rn 
          from AAA 
    )
    group by rn
    order by rn 

    如果没有分类,可以加一列分组

    select max(case when lev = 1 then name end) as lv1, 
           max(case when lev = 2 then name end) as lv2 , 
           max(case when lev = 3 then name end) as lv3 
    from
      (
      select name ,yield ,
      --对数据分组
      case when rn <=16 then 1 when rn <= 32 then 2 else 3 end lev 
      --不同分组编号
      ,row_number() over(partition by (case when rn <= 16 then 1 when rn <= 32 then 2 else 3 end ) order by yield desc) rn
      from 
          (
          select name,yield 
          --,row_number() over(partition by name order by yield desc) rn
          ,dense_rank() over(order by yield desc) rn 
          from AAA
          )
      )
    group by rn 
    --order by rn 
    

     

  • 相关阅读:
    C# 停止或开启服务
    微软企业库 缓存
    C# 获取图片一部分
    [转载]MVC3在win 2003虚拟主机上的调试
    mongodb查询的语法
    Mongodb亿级数据量的性能测试比较完整收藏一下
    正则验证数字
    收到了Gmail的Beta测试邀请
    C#新手经验点滴
    Windows消息机制
  • 原文地址:https://www.cnblogs.com/suni/p/11120427.html
Copyright © 2020-2023  润新知