建立交叉報表(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;