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

  • 相关阅读:
    flex 鼠标事件和效果
    查看和修改Oracle服务器端的字符集
    Oracle随机抽取数据sql
    hibernate.properties
    Oracle11g物理文件冷备份(转)
    支付宝接口使用文档说明 支付宝异步通知(notify_url)与return_url
    关于Oracle的行转列
    Intellij idea 12 tomcat日志窗口不显示问题
    在Servlet中获取来源URL
    uniapp 实现小程序列表项左滑菜单
  • 原文地址:https://www.cnblogs.com/zhangwei595806165/p/2628307.html
Copyright © 2011-2022 走看看