zoukankan      html  css  js  c++  java
  • sql行转列实例

    select  gh  ,xm  ,
    max(A.bz) as bz  ,
    max(A.jcz) as jcz   ,
    max(A.dl) as dl  ,
    max(A.czzx) as czzx  ,
    max(A.jt) as jt  ,
    max(A.dt) as dt  ,
    max(A.jl) as jl  ,
    max(A.zycqbw) as zycqbw  ,
    max(A.zc) as zc  ,
    max(A.zxqbw) as zxqbw  ,
    max(A.lczx) as lczx
    from(  
    select  gh  ,xm  ,
    CASE bgzlmc when '编组'then sum(cworkcount) else 0 end as bz  ,
    CASE bgzlmc when '解车组'then sum(cworkcount) else 0 end as jcz  ,
    CASE bgzlmc when '代量'then sum(cworkcount) else 0 end as dl  ,
    CASE bgzlmc when '车组转线'then sum(cworkcount) else 0 end as czzx  ,
    CASE bgzlmc when '解体'then sum(cworkcount) else 0 end as jt  ,
    CASE bgzlmc when '倒途'then sum(cworkcount) else 0 end as dt  ,
    CASE bgzlmc when '禁溜'then sum(cworkcount) else 0 end as jl  ,
    CASE bgzlmc when '作业车取摆位'then sum(cworkcount) else 0 end as zycqbw  ,
    CASE bgzlmc when '转场'then sum(cworkcount) else 0 end as zc  ,
    CASE bgzlmc when '站修取摆位'then sum(cworkcount) else 0 end as zxqbw  ,
    CASE bgzlmc when '列车转线'then sum(cworkcount) else 0 end as lczx 
     from t_dcd_confirmworkdetail  left join t_dcd_personworkdetails  
      on t_dcd_confirmworkdetail.planid = t_dcd_personworkdetails.planid   
      left join t_dcd_bgzl   on t_dcd_confirmworkdetail.bgzlid =t_dcd_bgzl.id 
       where 1=1    and ctime between '2018-09-01' and '2018-09-19'   group by gh, xm,bgzlmc )A   group by gh,xm  
    

      转换前数据:

        转换后数据:

    萌橙 你瞅啥?
  • 相关阅读:
    poj 2312 Battle City
    poj 2002 Squares
    poj 3641 Pseudoprime numbers
    poj 3580 SuperMemo
    poj 3281 Dining
    poj 3259 Wormholes
    poj 3080 Blue Jeans
    poj 3070 Fibonacci
    poj 2887 Big String
    poj 2631 Roads in the North
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/9674939.html
Copyright © 2011-2022 走看看