SQL查詢一年的十二個月份,形成報表
SELECT YEAR(日期欄位) 年度,SUM(CASE WHEN MONTH(日期欄位) =1 THEN 統計的欄位 ELSE 0 END) 一月,
SUM(CASE WHEN MONTH(日期欄位) =2 THEN 統計的欄位 ELSE 0 END) 二月,
SUM(CASE WHEN MONTH(日期欄位) =3 THEN 統計的欄位 ELSE 0 END) 三月,
SUM(CASE WHEN MONTH(日期欄位) =4 THEN 統計的欄位 ELSE 0 END) 四月,
SUM(CASE WHEN MONTH(日期欄位) =5 THEN 統計的欄位 ELSE 0 END) 五月,
SUM(CASE WHEN MONTH(日期欄位) =6 THEN 統計的欄位 ELSE 0 END) 六月,
SUM(CASE WHEN MONTH(日期欄位) =7 THEN 統計的欄位 ELSE 0 END) 七月,
SUM(CASE WHEN MONTH(日期欄位) =8 THEN 統計的欄位 ELSE 0 END) 八月,
SUM(CASE WHEN MONTH(日期欄位) =9 THEN 統計的欄位 ELSE 0 END) 九月,
SUM(CASE WHEN MONTH(日期欄位) =10 THEN 統計的欄位 ELSE 0 END) 十月,
SUM(CASE WHEN MONTH(日期欄位) =11 THEN 統計的欄位 ELSE 0 END) 十一月,
SUM(CASE WHEN MONTH(日期欄位) =12 THEN 統計的欄位 ELSE 0 END) 十二月,
FROM 表
GROUP BY YEAR(日期欄位)
Oracle統計某一年中的1-12個月的資料總和
實戰案例
查詢2015年1月到12個月的所有實有人口數量和往年2014年1月到12個月的實有人口數量,沒有的月份顯示 人口數量為0.類似效果如圖
建立表
create table PERSONSITUATION
(
id NUMBER not null,
rdate DATE,
nums NUMBER
)
插入資料
insert into PERSONSITUATION (id, rdate, nums) values (1, to_date('26-01-2015', 'dd-mm-yyyy'), 131);
insert into PERSONSITUATION (id, rdate, nums) values (2, to_date('27-01-2013', 'dd-mm-yyyy'), 232);
insert into PERSONSITUATION (id, rdate, nums) values (3, to_date('18-10-2013', 'dd-mm-yyyy'), 222);
insert into PERSONSITUATION (id, rdate, nums) values (4, to_date('20-01-2015', 'dd-mm-yyyy'), 232);
insert into PERSONSITUATION (id, rdate, nums) values (5, to_date('28-01-2015', 'dd-mm-yyyy'), 422);
insert into PERSONSITUATION (id, rdate, nums) values (6, to_date('26-02-2015', 'dd-mm-yyyy'), 232);
insert into PERSONSITUATION (id, rdate, nums) values (7, to_date('29-01-2014', 'dd-mm-yyyy'), 225);
insert into PERSONSITUATION (id, rdate, nums) values (8, to_date('31-01-2015', 'dd-mm-yyyy'), 111);
insert into PERSONSITUATION (id, rdate, nums) values (9, to_date('25-01-2013', 'dd-mm-yyyy'), 211);
insert into PERSONSITUATION (id, rdate, nums) values (10, to_date('25-01-2013', 'dd-mm-yyyy'), 251);
insert into PERSONSITUATION (id, rdate, nums) values (11, to_date('25-01-2013', 'dd-mm-yyyy'), 262);
insert into PERSONSITUATION (id, rdate, nums) values (12, to_date('25-08-2015', 'dd-mm-yyyy'), 233);
insert into PERSONSITUATION (id, rdate, nums) values (13, to_date('25-01-2013', 'dd-mm-yyyy'), 211);
insert into PERSONSITUATION (id, rdate, nums) values (14, to_date('25-02-2014', 'dd-mm-yyyy'), 222);
insert into PERSONSITUATION (id, rdate, nums) values (15, to_date('25-03-2012', 'dd-mm-yyyy'), 209);
insert into PERSONSITUATION (id, rdate, nums) values (16, to_date('25-01-2012', 'dd-mm-yyyy'), 219);
常見的統計中如下Sql語句,只查詢到了資料中有的月,如果沒有就什麼也沒有顯示,和明顯不符合1-12個月的12條資料統計
select to_char(rdate,'yyyy-mm') rdate,sum(nums) nums from personsituation where to_char(rdate,'yyyy')='2015' group by to_char(rdate,'yyyy-mm') order by rdate
正確的分析是:必須是12條資料,而且是統計的結果的12條資料,從這裡入手編寫如下Sql語句,顯示12個列的統計資料,先查詢出一年的資料,然後再連線另外一條資料拼接
select sum(decode(to_char(rdate,'mm'),'01',nums,0)) nums01,sum(decode(to_char(rdate,'mm'),'02',nums,0)) nums02,
sum(decode(to_char(rdate,'mm'),'03',nums,0)) nums03,sum(decode(to_char(rdate,'mm'),'04',nums,0)) nums04,
sum(decode(to_char(rdate,'mm'),'05',nums,0)) nums05,sum(decode(to_char(rdate,'mm'),'06',nums,0)) nums06,
sum(decode(to_char(rdate,'mm'),'07',nums,0)) nums07,sum(decode(to_char(rdate,'mm'),'08',nums,0)) nums08,
sum(decode(to_char(rdate,'mm'),'09',nums,0)) nums09,sum(decode(to_char(rdate,'mm'),'10',nums,0)) nums10,
sum(decode(to_char(rdate,'mm'),'11',nums,0)) nums11,sum(decode(to_char(rdate,'mm'),'12',nums,0)) nums12
from personsituation
where to_char(rdate,'yyyy')='2015'
而前端頁面顯示的結果有兩種可能性,一種是橫向展示,一種是縱向展示,可以使用下面的sql語句進行列轉行的轉換得到如下結果
select * from (
select sum(decode(to_char(rdate,'mm'),'01',nums,0)) nums01,sum(decode(to_char(rdate,'mm'),'02',nums,0)) nums02,
sum(decode(to_char(rdate,'mm'),'03',nums,0)) nums03,sum(decode(to_char(rdate,'mm'),'04',nums,0)) nums04,
sum(decode(to_char(rdate,'mm'),'05',nums,0)) nums05,sum(decode(to_char(rdate,'mm'),'06',nums,0)) nums06,
sum(decode(to_char(rdate,'mm'),'07',nums,0)) nums07,sum(decode(to_char(rdate,'mm'),'08',nums,0)) nums08,
sum(decode(to_char(rdate,'mm'),'09',nums,0)) nums09,sum(decode(to_char(rdate,'mm'),'10',nums,0)) nums10,
sum(decode(to_char(rdate,'mm'),'11',nums,0)) nums11,sum(decode(to_char(rdate,'mm'),'12',nums,0)) nums12
from personsituation
where to_char(rdate,'yyyy')='2015'
) unpivot (sum2015 for years in (nums01,nums02,nums03,nums04,nums05,nums06,nums07,nums08,nums09,nums10,nums11,nums12) )
最後一步就是和往年的資料對比使用left join
查詢
select A.years,A.SUM2015,B.SUM2014 from
(select * from (
select sum(decode(to_char(rdate,'mm'),'01',nums,0)) nums01,sum(decode(to_char(rdate,'mm'),'02',nums,0)) nums02,
sum(decode(to_char(rdate,'mm'),'03',nums,0)) nums03,sum(decode(to_char(rdate,'mm'),'04',nums,0)) nums04,
sum(decode(to_char(rdate,'mm'),'05',nums,0)) nums05,sum(decode(to_char(rdate,'mm'),'06',nums,0)) nums06,
sum(decode(to_char(rdate,'mm'),'07',nums,0)) nums07,sum(decode(to_char(rdate,'mm'),'08',nums,0)) nums08,
sum(decode(to_char(rdate,'mm'),'09',nums,0)) nums09,sum(decode(to_char(rdate,'mm'),'10',nums,0)) nums10,
sum(decode(to_char(rdate,'mm'),'11',nums,0)) nums11,sum(decode(to_char(rdate,'mm'),'12',nums,0)) nums12
from personsituation
where to_char(rdate,'yyyy')='2015'
) unpivot (sum2015 for years in (nums01,nums02,nums03,nums04,nums05,nums06,nums07,nums08,nums09,nums10,nums11,nums12) )) A
left join
(select * from (
select sum(decode(to_char(rdate,'mm'),'01',nums,0)) nums01,sum(decode(to_char(rdate,'mm'),'02',nums,0)) nums02,
sum(decode(to_char(rdate,'mm'),'03',nums,0)) nums03,sum(decode(to_char(rdate,'mm'),'04',nums,0)) nums04,
sum(decode(to_char(rdate,'mm'),'05',nums,0)) nums05,sum(decode(to_char(rdate,'mm'),'06',nums,0)) nums06,
sum(decode(to_char(rdate,'mm'),'07',nums,0)) nums07,sum(decode(to_char(rdate,'mm'),'08',nums,0)) nums08,
sum(decode(to_char(rdate,'mm'),'09',nums,0)) nums09,sum(decode(to_char(rdate,'mm'),'10',nums,0)) nums10,
sum(decode(to_char(rdate,'mm'),'11',nums,0)) nums11,sum(decode(to_char(rdate,'mm'),'12',nums,0)) nums12
from personsituation
where to_char(rdate,'yyyy')='2014'
) unpivot (sum2014 for years in (nums01,nums02,nums03,nums04,nums05,nums06,nums07,nums08,nums09,nums10,nums11,nums12) )) B
on A.years = B.years
實現與效果圖一樣的資料,可以將sql中的nums01...nums12
改為1月...12月
select A.years,A.SUM1,B.SUM2 from
(select * from (
select sum(decode(to_char(rdate,'mm'),'01',nums,0)) 一月,sum(decode(to_char(rdate,'mm'),'02',nums,0)) 二月,
sum(decode(to_char(rdate,'mm'),'03',nums,0)) 三月,sum(decode(to_char(rdate,'mm'),'04',nums,0)) 四月,
sum(decode(to_char(rdate,'mm'),'05',nums,0)) 五月,sum(decode(to_char(rdate,'mm'),'06',nums,0)) 六月,
sum(decode(to_char(rdate,'mm'),'07',nums,0)) 七月,sum(decode(to_char(rdate,'mm'),'08',nums,0)) 八月,
sum(decode(to_char(rdate,'mm'),'09',nums,0)) 九月,sum(decode(to_char(rdate,'mm'),'10',nums,0)) 十月,
sum(decode(to_char(rdate,'mm'),'11',nums,0)) 十一月,sum(decode(to_char(rdate,'mm'),'12',nums,0)) 十二月
from personsituation
where to_char(rdate,'yyyy')= '2015'
) unpivot (sum1 for years in (一月,二月,三月,四月,五月,六月,七月,八月, 九月,十月,十一月,十二月) )) A
left join
(select * from (
select sum(decode(to_char(rdate,'mm'),'01',nums,0)) 一月,sum(decode(to_char(rdate,'mm'),'02',nums,0)) 二月,
sum(decode(to_char(rdate,'mm'),'03',nums,0)) 三月,sum(decode(to_char(rdate,'mm'),'04',nums,0)) 四月,
sum(decode(to_char(rdate,'mm'),'05',nums,0)) 五月,sum(decode(to_char(rdate,'mm'),'06',nums,0)) 六月,
sum(decode(to_char(rdate,'mm'),'07',nums,0)) 七月,sum(decode(to_char(rdate,'mm'),'08',nums,0)) 八月,
sum(decode(to_char(rdate,'mm'),'09',nums,0)) 九月,sum(decode(to_char(rdate,'mm'),'10',nums,0)) 十月,
sum(decode(to_char(rdate,'mm'),'11',nums,0)) 十一月,sum(decode(to_char(rdate,'mm'),'12',nums,0)) 十二月
from personsituation
where to_char(rdate,'yyyy')= '2014'
) unpivot (sum2 for years in (一月,二月,三月,四月,五月,六月,七月,八月, 九月,十月,十一月,十二月) )) B
on A.years = B.years
相關文章
- SQL 兩個表組合查詢SQL
- SQL裡3個表的連線查詢SQL
- SQL多個表實現聯合查詢SQL
- SAP RETAIL分配表的查詢報表AI
- 查詢全表掃描的sqlSQL
- [MySQL] - 聯表查詢,查詢一個不在另一個表的記錄MySql
- SQL 三表聯合查詢SQL
- 複雜SQL查詢和視覺化報表構建SQL視覺化
- GP查詢外部表報錯
- SQL語句查詢表結構SQL
- 【資料庫】sql連表查詢資料庫SQL
- SQL三表左關聯查詢SQL
- mysql 查詢建表語句sqlMySql
- SQL總結(二)連表查詢SQL
- 兩表關聯查詢:sql、mybatisSQLMyBatis
- 查詢某個表的索引資訊索引
- ORACLE SQL的查詢表的鎖的情況OracleSQL
- SQL查詢的:子查詢和多表查詢SQL
- 一個sql查詢語句報ORA-01722SQL
- 幾個SQL查詢小技巧SQL
- 報表怎麼做模糊查詢
- SQL server根據表名查詢表主鍵SQLServer
- sql查詢一張表的重複資料SQL
- 有關表空間查詢的sql指令碼SQL指令碼
- SQL單表查詢語句總結SQL
- SQL面試題 三(單表、多表查詢)SQL面試題
- Oracle 表空間查詢相關sqlOracleSQL
- 查詢 SQL SERVER 所有表記錄數SQLServer
- 查詢一個表的外來鍵
- Oracle分頁查詢格式(十二)Oracle
- sql 連線查詢例項(left join)三表連線查詢SQL
- 幾個定位、查詢session的sql語句SessionSQL
- 報表查詢條件的 N 種使用方式
- django 兩個表或多個表聯合查詢Django
- sql 查詢當前使用者所有表的容量SQL
- SQL Server 查詢表註釋和欄位SQLServer
- 子查詢-表子查詢
- 查詢一個表的一列插入到另一個表