zoukankan      html  css  js  c++  java
  • 小试下新博客,一个列传行的SQL

    源表的结构如下:

    create table TAB_SAL
    (
      empno         NUMBER,
      income_type   NUMBER,
      income_amount NUMBER
    )

    列转行SQL:

     1 select t.empno,
     2        (select sum(t1.income_amount)
     3           from TAB_SAL t1
     4          where t1.income_type = 11
     5            and t1.empno = t.empno),
     6        (select sum(t1.income_amount)
     7           from TAB_SAL t1
     8          where t1.income_type = 22
     9            and t1.empno = t.empno),
    10        (select sum(t1.income_amount)
    11           from TAB_SAL t1
    12          where t1.income_type = 33
    13            and t1.empno = t.empno),
    14        (select sum(t1.income_amount)
    15           from TAB_SAL t1
    16          where t1.income_type = 44
    17            and t1.empno = t.empno)
    18 
    19   from TAB_SAL t
    20  group by t.empno
     1 select t.empno,
     2        (select t1.income_amount
     3           from TAB_SAL t1
     4          where t1.empno = t.empno
     5            and t1.income_type = 11),
     6        (select t1.income_amount
     7           from TAB_SAL t1
     8          where t1.empno = t.empno
     9            and t1.income_type = 22),
    10        (select t1.income_amount
    11           from TAB_SAL t1
    12          where t1.empno = t.empno
    13            and t1.income_type = 33),
    14        (select t1.income_amount
    15           from TAB_SAL t1
    16          where t1.empno = t.empno
    17            and t1.income_type = 44)
    18 
    19   from TAB_SAL t
    20 
    21  group by t.empno

    一个加sum一个不加,结果都可以,我认为分组完毕后select后面只能出现每组最小维的一行数据,只要保证这个我觉得就可以。

  • 相关阅读:
    http请求的跨域
    事件驱动模型
    单例设计
    引用类型与垃圾回收
    注解开发
    Java的反射机制
    Android Listener 监听的几种写法
    人生必须有所规划,工作是好比马拉松
    Android 常用的常量
    Android 常见的广播 action常量
  • 原文地址:https://www.cnblogs.com/JSD1207ZX/p/9378795.html
Copyright © 2011-2022 走看看