数据库表转置查询

 
一、SQL Server 中的转置

SELECT Name, SUM(CASE WHEN Subject = '语文' THEN Result END) AS 语文,
      SUM(CASE WHEN Subject = '数学' THEN Result END) AS 数学,
      SUM(CASE WHEN Subject = '英语' THEN Result END) AS 英语
FROM CT
GROUP BY Name


 

二、Oracle中的转置

现有一个商品销售表sale,表结构为:

month   char(6)     --月份

sell    number(10,2)   --月销售金额

现有数据为:

200001  1000

200002  1100

200003  1200

200004  1300

200005  1400

200006  1500

200007  1600

200101  1100

200202  1200

200301  1300

想要转化为以下结构的数据:

year   char(4)     --年份

month1  number(10,2)   --1月销售金额

month2  number(10,2)   --2月销售金额

month3  number(10,2)   --3月销售金额

month4  number(10,2)   --4月销售金额

month5  number(10,2)   --5月销售金额

month6  number(10,2)   --6月销售金额

month7  number(10,2)   --7月销售金额

month8  number(10,2)   --8月销售金额

month9  number(10,2)   --9月销售金额

month10  number(10,2)   --10月销售金额

month11  number(10,2)   --11月销售金额

month12  number(10,2)   --12月销售金额

结构转化的SQL语句为:

create or replace view

v_sale(year,month1,month2,month3,month4,month5,month6,month7,month8,month9,month10,month11,month12)

as

    select

    substrb(month,1,4),

    sum(decode(substrb(month,5,2),'01',sell,0)),

    sum(decode(substrb(month,5,2),'02',sell,0)),

    sum(decode(substrb(month,5,2),'03',sell,0)),

    sum(decode(substrb(month,5,2),'04',sell,0)),

    sum(decode(substrb(month,5,2),'05',sell,0)),

    sum(decode(substrb(month,5,2),'06',sell,0)),

    sum(decode(substrb(month,5,2),'07',sell,0)),

    sum(decode(substrb(month,5,2),'08',sell,0)),

    sum(decode(substrb(month,5,2),'09',sell,0)),

    sum(decode(substrb(month,5,2),'10',sell,0)),

    sum(decode(substrb(month,5,2),'11',sell,0)),

    sum(decode(substrb(month,5,2),'12',sell,0))

    from sale

    group by substrb(month,1,4);

 

 

oracle sql语句转置表

想把表1转成表2,仅考虑用sql语句实现,谢谢。

select col1,col2,sum(decode(col3,'X',col4)) X

,sum(decode(col3,'Y',col4)) Y

,sum(decode(col3,'Z',col4)) Z

from tablename

group by col1,col2

 

其他回答 2 

 

select a.1,a.2,a.4 as X,null as Y,null as Z from 1 as a where a.3 = 'X'

union all

select b.1,b.2,null as X,b.4 as Y,null as Z from 1 as b where b.3 = 'Y'

union all

select c.1,c.2,null as X,null as Y,c.4 as Z from 1 as c where c.3 = 'Z' 

 

select 1,2,

(case when 3='x' then 4 end) as x,(case when 3='y' then 4 end) as y,(case when 3='z' then 4 end) as z from 

你要显示的漂亮一点就加:else ' '

select 1,2,

(case when 3='x' then 4 else ' 'end) as x,

(case when 3='y' then 4 else ' ' end) as y,

(case when 3='z' then 4 else ' ' end) as z 

from 

 

:住房公积金报表置换实例:
  1.各个单位在本地经办行进行开户,开户就是将单位的基本信息和职工信息的进行登记;
  2.每月各个单位的会计到经办行交缴本单位的所有职工的住房公积金,系统记录有每个职工的交缴明细并在每条记录上记录有经办行的代码;
  3.每月、季、半年及年终都要求将经办行 变为给出个月的明细报表:
  经办行:城西区 城东区
  月份:
  2001.01 xxxx1.xx xxxxx2.xx
  2001.02 xxxx3.xx xxxxx4.xx
  。     
  原来的数据顺序是:
  城西区2001.01 xxxxx1.xx
  城东区2001.01 xxxxx2.xx
  城西区2001.02 xxxxx3.xx
  城东区2001.02 xxxxx4.xx
  住房公积金系统记录职工的每月交缴名细的pay_lst表结构是:
  

Sql代码

  bank_code varchar2(6)NOT NULL, -- 经办行代码    acc_no varchar2(15) not null, -- 单位代码(单位帐号)    emp_acc_no varchar2(20) not null, -- 职工帐号    tran_date date not null, -- 交缴日期    tran_val Number(7,2) not null, -- 交缴额     sys_date date default sysdate, --系统日期    oper_id varchar2(10) --操作员代码    bank_code varchar2(6)NOT NULL, --经办行代码  acc_no varchar2(15) not null, --单位代码(单位帐号)  emp_acc_no varchar2(20) not null, --职工帐号  tran_date date not null, --交缴日期  tran_val Number(7,2) not null, --交缴额  sys_date date default sysdate, --系统日期  oper_id varchar2(10) --操作员代码


 
  
这样的表结构,一般按照将经办行作为行(row)进行统计是很容易的,
但是如果希望将经办行变为列(column)这样的格式来输出就有困难。
如果用DECODE函数来处理则变得很简单:
我们创建一个视图来对目前的pay_lst表进行查询。
将经办行代码变为一些具体的经办行名称即可: 

Sql代码

CREATE OR REPLACE VIEW bank_date_lst AS    Select to_char(tran_date,’yyyy.mm’),     SUM( DECODE ( bank_code,’001’, tran_val,0 )) 城西区,     SUM( DECODE ( bank_code,’002’, tran_val,0 )) 城南区,     SUM( DECODE ( bank_code,’003’, tran_val,0 )) 城东区     FROM pay_lst     GROUP BY to_char(tran_date,’yyyy.mm’);  CREATE OR REPLACE VIEW bank_date_lst AS  Select to_char(tran_date,’yyyy.mm’),  SUM( DECODE ( bank_code,’001’, tran_val,0 ))城西区,  SUM( DECODE ( bank_code,’002’, tran_val,0 ))城南区,  SUM( DECODE ( bank_code,’003’, tran_val,0 ))城东区  FROM pay_lst  GROUP BY to_char(tran_date,’yyyy.mm’);


建立视图后,可直接对该视图进行查询就可按照列显示出结果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章