zoukankan      html  css  js  c++  java
  • TSQL行转列、列转行

    这是原表:

    想实现这种效果:

    过程我就不写了,把原代码贴给大家:

     1 select (select qhmc from tab_qh c where c.qhid=b.sjqhbh) sjqh ,qhmc,a.lx,'全县平均值' as fl,
     2 max(case zbbh when '1' then Pjz else 0 end) as bh1,
     3 max(case zbbh when '2' then Pjz else 0 end) as bh2,
     4 max(case zbbh when '3' then Pjz else 0 end) as bh3,
     5 max(case zbbh when '4' then Pjz else 0 end) as bh4,
     6 max(case zbbh when '5' then Pjz else 0 end) as bh5,
     7 max(case zbbh when '6' then Pjz else 0 end) as bh6,
     8 max(case zbbh when '7' then Pjz else 0 end) as bh7,
     9 max(case zbbh when '8' then Pjz else 0 end) as bh8,
    10 max(case zbbh when '9' then Pjz else 0 end) as bh9
    11 from tab_xjjhpg a inner join  tab_qh b on a.qhid=b.qhid
    12 where nf='2012'
    13 group by b.sjqhbh,qhmc,a.lx
    14 union
    15 select (select qhmc from tab_qh c where c.qhid=b.sjqhbh) sjqh ,qhmc,a.lx,'差异系数' as fl,
    16 max(case zbbh when '1' then cyxs else 0 end) as bh1,
    17 max(case zbbh when '2' then cyxs else 0 end) as bh2,
    18 max(case zbbh when '3' then cyxs else 0 end) as bh3,
    19 max(case zbbh when '4' then cyxs else 0 end) as bh4,
    20 max(case zbbh when '5' then cyxs else 0 end) as bh5,
    21 max(case zbbh when '6' then cyxs else 0 end) as bh6,
    22 max(case zbbh when '7' then cyxs else 0 end) as bh7,
    23 max(case zbbh when '8' then cyxs else 0 end) as bh8,
    24 max(case zbbh when '9' then cyxs else 0 end) as bh9
    25 from tab_xjjhpg a inner join  tab_qh b on a.qhid=b.qhid
    26 where nf='2012'
    27 group by b.sjqhbh,qhmc,a.lx
    28 order by qhmc,sjqh

     注意点:1、使用mask-case

         2、union联合查询

         3、添加一个字段,这里我添加的是'全县平均值'as fl 和'差异系数' as fl

  • 相关阅读:
    使用批处理查找某个文件夹及其子文件夹(递归地)中存在的空文件夹
    pl/sql连接oracle
    JSON对象转换成url参数
    创建maven工程
    线程同步
    java.lang.Object 的 wait 和 notify方法,及关键字 synchronized的作用
    Thread操作
    java.lang.io包的使用
    用程序打印汉字
    java程序员应该熟练掌握的技术
  • 原文地址:https://www.cnblogs.com/zhangwei595806165/p/2628307.html
Copyright © 2011-2022 走看看