建立交叉報表(oracle)

來源:互聯網
上載者:User
oracle|建立
建立交叉報表

create table t1(
goodid  number(10)  not null,
saledate  date   not null,
salesum  number(10)
);

要求產生本年度每個月的產品銷售狀況表

 m1 m2 m3 ... m12
g1
g2
.
.
.
gn

下面是產生報表的sql

SELECT goodid,
 SUM(decode(to_char(saledate,'mm'),'01',salesum)) "01",
 SUM(decode(to_char(saledate,'mm'),'02',salesum)) "02",
 SUM(decode(to_char(saledate,'mm'),'03',salesum)) "03",
 SUM(decode(to_char(saledate,'mm'),'04',salesum)) "04",
 SUM(decode(to_char(saledate,'mm'),'05',salesum)) "05",
 SUM(decode(to_char(saledate,'mm'),'06',salesum)) "06",
 SUM(decode(to_char(saledate,'mm'),'07',salesum)) "07",
 SUM(decode(to_char(saledate,'mm'),'08',salesum)) "08",
 SUM(decode(to_char(saledate,'mm'),'09',salesum)) "09",
 SUM(decode(to_char(saledate,'mm'),'10',salesum)) "10",
 SUM(decode(to_char(saledate,'mm'),'11',salesum)) "11",
 SUM(decode(to_char(saledate,'mm'),'12',salesum)) "12"
from t1
where to_char(saledate,'yyyy') = '2004'
group by goodid
order by goodid;

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.