前兩天,組長給我了一個模塊,讓我按條件統計一下一個表中的數據,並按條件顯示出來,聽着沒有什麼,再說我之前所說也作過相似的東西,不過仔細看了一下給的這個表,發現之前的東西不夠用了。sql
如今我把這個表中的主要字段給出:數據庫
表名:recordapp
序號jsp |
字段名工具 |
字段類型spa |
中文含義.net |
說明code |
1orm |
record_idblog |
VARCHAR2 (32) |
記錄編號 |
primary key |
2 |
cycle_no |
VARCHAR2 (32) |
事項編號,取值只能取右邊的幾個,我只給出了數值,至於具體表明什麼含義,就不寫了 |
001 002 003 004 005 006 |
3 |
record_type |
NUMBER(2) |
處理類型 |
10/20/30/40 受理/審查/決定/告知 DEFAULT -1 |
4 |
record_result |
NUMBER(1) |
處理結果 |
0/1 成功/失敗 default -1(未操做) |
5 |
accept_op |
VARCHAR2(32) |
受理人 |
與user表關聯,user_id |
6 |
accept_date |
DATE |
受理時間 |
|
18 |
check_op |
VARCHAR2(32) |
審查人 |
與user表關聯,user_id |
19 |
check_date |
DATE |
審查時間 |
|
21 |
choose_op |
VARCHAR2(32) |
決定人 |
與user表關聯,user_id |
22 |
choose_date |
DATE |
決定時間 |
|
24 |
apprise_op |
VARCHAR2(32) |
告知人 |
與user表關聯,user_id |
25 |
apprise_date |
DATE |
告知時間 |
|
表User,咱們主要用user_id,user_name,這個就不用說了
統計的條件是
統計時間段: start_date end_date]
統計事項: cycle_no (check_box默認都選中)
統計分類:年、月、日 stat_type (統計報表不一樣、默認日)
是否按人員分類:是、否 is_user (統計報表不一樣、默認否)
要我給寫出統計結果,頁面大體以下
事項 統計時間 |
|
001 |
002 |
……其它項 |
|||||||
處理人 |
受理 |
審查 |
決定 |
告知 |
受理 |
審查 |
決定 |
告知 |
|
||
2007-10-20 |
陳** |
10 |
20 |
14 |
15 |
15 |
12 |
34 |
62 |
|
|
……其它人員 |
|
|
|
|
|
|
|
|
|
||
…… 按日期排列 |
陳** |
10 |
20 |
14 |
15 |
15 |
12 |
34 |
62 |
|
|
……其它人員 |
|
|
|||||||||
合計 |
陳** |
此列合計 |
|
|
|
|
|
|
|
|
|
……其它人員 |
|
|
|
|
|
|
|
|
|
||
總計 |
陳** |
四列合計 |
|
|
|||||||
……其它人員 |
|
|
|
||||||||
合計 |
全部人員四列合計 |
|
|
||||||||
注:
受理統計時:record_type > 受理、或者record_type = 受理 且 受理成功(record_result=0)的數據,
審查統計時:record_type > 審查、或者record_type = 審查 且 審查成功(record_result=0)的數據,
決定統計時:record_type > 決定、或者record_type = 決定 且 決定成功(record_result=0)的數據,
告知統計時: record_type = 告知 且 告知成功(record_result=1)的數據的
即若是一行的record_type 是告知時,說明該行已經進行了受理,審查和決定
我在網上查了一下,僅發現如下東西對我比較有參考價值
<!--[if !supportLists]-->1) <!--[endif]-->使用CUBE和ROLLUP對數據進行彙總 ,SQL語言中的ROLLUP和CUBE命令提供了一個很是有用的工具,可讓您快速深刻地獲取數據的各類內在性質。ROLLUP和CUBE是SQL的擴展命令,能夠在SQL Server 6.5(及以上版本)和Oracle 8i(及以上版本)中使用。具體見本博客的《使用CUBE和ROLLUP對數據進行彙總》篇
<!--[if !supportLists]-->2) <!--[endif]-->使用decode /group by/ order by/sign/sum來實現不一樣報表的生成,具體見本博客的《SQL行列轉換實戰 》篇
不過我沒有想到的是,在Oracle中,對於Cube和Rollup使用,是group by cube(deal_date,deal_op),而不是SQL Server中的GROUP BY deal_date,deal_op WITH Cube/Rollup
最後我仔細考慮了一下,把這個統計頁面的顯示分紅了如下幾步:
<!--[if !supportLists]-->一、 <!--[endif]-->創建視圖:視圖中有五項,分別爲處理時間,處理人,事項編號,處理類型,處理數量
注:
1)步中,對於record_type的處理,是直接設置爲對應的值,這樣的話在之後查詢中,不用再加
RECORD_TYPE>record_type OR (RECORD_TYPE=record_type and RECORD_RESULT=0)
的判斷,直接用record_type=value就能夠了
2)在該步中,對日期用了trunc(accept_date,'DD'),將數據庫中的日期取到天項,這樣是爲了按日,月,年統計時方便
原SQL代碼以下:
create or replace view v_censorial_report(deal_date,deal_op,cycle_no,record_type,type_num)
as
select trunc(accept_date,'DD'),accept_op,cycle_no,10 as record_type,count(record_id)
from t_censorial_cycle_record
where (RECORD_TYPE > 10 OR (RECORD_TYPE=10 and RECORD_RESULT=0)) and accept_op is not null
group by trunc(accept_date,'DD'),accept_op,cycle_no
union
select trunc(check_date,'DD'),check_op,cycle_no,20 as record_type,count(record_id)
from t_censorial_cycle_record
where (RECORD_TYPE>20 OR (RECORD_TYPE=20 and RECORD_RESULT=0)) and check_op is not null
group by trunc(check_date,'DD'),check_op,cycle_no
union
select trunc(choose_date,'DD'),choose_op,cycle_no,30 as record_type,count(record_id)
from t_censorial_cycle_record
where (
RECORD_TYPE>30 OR (RECORD_TYPE=30 and RECORD_RESULT=0)) and choose_op is not null
group by trunc(choose_date,'DD'),choose_op,cycle_no
union
select trunc(apprise_date,'DD'),apprise_op,cycle_no,40 as record_type,count(record_id)
from t_censorial_cycle_record
where (RECORD_TYPE>40 OR (RECORD_TYPE=40 and RECORD_RESULT=0)) and apprise_op is not null
group by trunc(apprise_date,'DD'),apprise_op,cycle_no
<!--[if !supportLists]-->二、 <!--[endif]-->獲得censorial_report_index.jsp中用戶所選擇的參數
<!--[if !supportLists]-->(1) <!--[endif]-->start_date 開始時間,該項不會爲空
<!--[if !supportLists]-->(2) <!--[endif]-->end_date 結束時間,該項不會爲空
<!--[if !supportLists]-->(3) <!--[endif]-->事項編號,從cycleNo1~cycleNo6
<!--[if !supportLists]-->(4) <!--[endif]-->stat_type 分類的類型(year/month/day)
<!--[if !supportLists]-->(5) <!--[endif]-->issuer是否按人員分類(true/false)
<!--[if !supportLists]-->三、 <!--[endif]-->分析統計條件,判斷結果表censorial_report_stat.jsp
注:
(1)對於按何種方式統計,能夠select語句中的deal_date進行修改
--按月: to_char(deal_date,'YYYYMM')
--按年 : to_char(deal_date,'YYYY')
(2)對於事項編號,能夠在select後的語句中,將所選中的列取出,在判斷時,只需在下邊的sql語句中加上編號就能夠了。
cycle_no = '001' or cycle_no = '002' or cycle_no = '003' or cycle_no = '004' or cycle_no = '005' or cycle_no = '006'
1)當isuser爲true,stat_type 爲day時
事項 統計時間 |
|
001 |
002 |
……其它項 |
||||||
處理人 |
受理 |
審查 |
決定 |
告知 |
受理 |
審查 |
決定 |
告知 |
|
|
2007-10-20 |
陳** |
10 |
20 |
14 |
15 |
15 |
12 |
34 |
62 |
|
……其它人員 |
|
|
|
|
|
|
|
|
|
|
…… 按日期排列 |
陳** |
10 |
20 |
14 |
15 |
15 |
12 |
34 |
62 |
|
……其它人員 |
|
|
|
|
|
|
|
|
|
|
合計 |
陳** |
此列合計 |
|
|
|
|
|
|
|
|
……其它人員 |
|
|
|
|
|
|
|
|
|
|
總計 |
陳** |
四列合計 |
|
|
||||||
……其它人員 |
|
|
|
|||||||
合計 |
全部人員四列合計 |
|
|
有上圖可知,該項須要獲得
對於處理人一項,可用處理人id,而後獲得處理人姓名便可
統計時間一列,須要獲得每列與右邊列的對應關係,即一列跨越多行,須要分別一個相應的n值
對於除總計行外的其他行,可用比較時間得出n
對於總計行,n就是總計行結果集的記錄數量+1
除總計行外,其他行能夠用一條sql所有查詢出來,結構以下
統計時間 |
處理人 |
001 |
002 |
……其它項 |
||||||
受理 |
審查 |
決定 |
告知 |
受理 |
審查 |
決定 |
告知 |
|
||
2007-10-20 |
11433 |
10 |
20 |
14 |
15 |
15 |
12 |
34 |
62 |
|
2007-10-20 |
11853 |
|
|
|
|
|
|
|
|
|
。。 |
。。。 |
|
|
|
|
|
|
|
|
|
2007-10-21 |
11433 |
10 |
20 |
14 |
15 |
15 |
12 |
34 |
62 |
|
2007-10-21 |
11853 |
|
|
|
|
|
|
|
|
|
。。。 |
。。。 |
|
|
|
|
|
|
|
|
|
|
11433 |
10 |
20 |
14 |
15 |
15 |
12 |
34 |
62 |
|
|
11853 |
|
|
|
|
|
|
|
|
|
|
|
30 |
60 |
42 |
45 |
45 |
36 |
42 |
186 |
|
代碼以下
select deal_date,deal_op,
sum(case when (record_type = 10 and cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_APPRISE_NUM,
sum(case when (record_type = 10 and cycle_no = '002') then type_num else 0 end) ZBWJBA_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '002') then type_num else 0 end) ZBWJBA_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '002') then type_num else 0 end) ZBWJBA_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '002') then type_num else 0 end) ZBWJBA_ACCEPT_NUM,
sum(case when (record_type = 10 and cycle_no = '003') then type_num else 0 end) ZGYSWJBA_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '003') then type_num else 0 end) ZGYSWJBA_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '003') then type_num else 0 end) ZGYSWJBA_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '003') then type_num else 0 end) ZGYSWJBA_APPRISE_NUM,
sum(case when (record_type = 10 and cycle_no = '004') then type_num else 0 end) ZBFSCB_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '004') then type_num else 0 end) ZBFSCB_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '004') then type_num else 0 end) ZBFSCB_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '004') then type_num else 0 end) ZBFSCB_APPRISE_NUM,
sum(case when (record_type = 10 and cycle_no = '005') then type_num else 0 end) HTBA_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '005') then type_num else 0 end) HTBA_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '005') then type_num else 0 end) HTBA_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '005') then type_num else 0 end) HTBA_APPRISE_NUM,
sum(case when (record_type = 10 and cycle_no = '006') then type_num else 0 end) ZBQKSMBG_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '006') then type_num else 0 end) ZBQKSMBG_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '006') then type_num else 0 end) ZBQKSMBG_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '006') then type_num else 0 end) ZBQKSMBG_APPRISE_NUM
from v_censorial_report
where deal_date >= TO_DATE('2007-10-01 00:00:00','YYYY-MM-DD HH24:MI:SS')
and deal_date <= TO_DATE('2007-10-31 23:59:59','YYYY-MM-DD HH24:MI:SS')
and(cycle_no = '001' or cycle_no = '002' or cycle_no = '003' or
cycle_no = '004' or cycle_no = '005' or cycle_no = '006')
group by cube(deal_date,deal_op)
對於總計行,結構以下
統計時間 |
處理人 |
001 |
002 |
……其它項 |
|
|
60 |
110 |
|
|
11853 |
10 |
50 |
|
|
11433 |
50 |
60 |
|
select deal_op,
sum(case when (cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_NUM,
sum(case when (cycle_no = '002') then type_num else 0 end) ZBWJBA_NUM ,
sum(case when (cycle_no = '003') then type_num else 0 end) ZGYSWJBA_NUM,
sum(case when (cycle_no = '004') then type_num else 0 end) ZBFSCB_NUM,
sum(case when (cycle_no = '005') then type_num else 0 end) HTBA_NUM,
sum(case when (cycle_no = '006') then type_num else 0 end) ZBQKSMBG_NUM
from v_censorial_report
where deal_date >= TO_DATE('2007-10-01 00:00:00','YYYY-MM-DD HH24:MI:SS')
and deal_date <= TO_DATE('2007-10-31 23:59:59','YYYY-MM-DD HH24:MI:SS')
and(cycle_no = '001' or cycle_no = '002' or cycle_no = '003' or
cycle_no = '004' or cycle_no = '005' or cycle_no = '006')
group by cube(deal_op)
2)當isuser爲false,stat_type 爲day時
事項 統計時間 |
招標方式抄報 |
招標人自行招標文件備案 |
……其它申報項 |
||||||
受理 |
審查 |
決定 |
告知 |
受理 |
審查 |
決定 |
告知 |
|
|
2007-10-20 |
10 |
20 |
14 |
15 |
15 |
12 |
34 |
62 |
|
…… 按日期排列 |
10 |
20 |
14 |
15 |
15 |
12 |
34 |
62 |
|
合計
|
此列合計 |
|
|
|
|
|
|
|
|
總計 |
四列合計 |
|
|
在按日排序的狀況下,除總計行外,其他行能夠用一條sql所有查詢出來,以下
select deal_date,
sum(case when (record_type = 10 and cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_APPRISE_NUM,
sum(case when (record_type = 10 and cycle_no = '002') then type_num else 0 end) ZBWJBA_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '002') then type_num else 0 end) ZBWJBA_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '002') then type_num else 0 end) ZBWJBA_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '002') then type_num else 0 end) ZBWJBA_ACCEPT_NUM,
sum(case when (record_type = 10 and cycle_no = '003') then type_num else 0 end) ZGYSWJBA_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '003') then type_num else 0 end) ZGYSWJBA_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '003') then type_num else 0 end) ZGYSWJBA_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '003') then type_num else 0 end) ZGYSWJBA_APPRISE_NUM,
sum(case when (record_type = 10 and cycle_no = '004') then type_num else 0 end) ZBFSCB_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '004') then type_num else 0 end) ZBFSCB_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '004') then type_num else 0 end) ZBFSCB_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '004') then type_num else 0 end) ZBFSCB_APPRISE_NUM,
sum(case when (record_type = 10 and cycle_no = '005') then type_num else 0 end) HTBA_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '005') then type_num else 0 end) HTBA_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '005') then type_num else 0 end) HTBA_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '005') then type_num else 0 end) HTBA_APPRISE_NUM,
sum(case when (record_type = 10 and cycle_no = '006') then type_num else 0 end) ZBQKSMBG_ACCEPT_NUM,
sum(case when (record_type = 20 and cycle_no = '006') then type_num else 0 end) ZBQKSMBG_CHECK_NUM,
sum(case when (record_type = 30 and cycle_no = '006') then type_num else 0 end) ZBQKSMBG_CHOOSE_NUM,
sum(case when (record_type = 40 and cycle_no = '006') then type_num else 0 end) ZBQKSMBG_APPRISE_NUM
from v_censorial_report
where deal_date >= TO_DATE('2007-10-01 00:00:00','YYYY-MM-DD HH24:MI:SS')
and deal_date <= TO_DATE('2007-10-31 23:59:59','YYYY-MM-DD HH24:MI:SS')
and(cycle_no = '001' or cycle_no = '002' or cycle_no = '003' or
cycle_no = '004' or cycle_no = '005' or cycle_no = '006')
group by cube(deal_date)
對於總計行
select
sum(case when (cycle_no = '001') then type_num else 0 end) ZBRZXTJBA_NUM,
sum(case when (cycle_no = '002') then type_num else 0 end) ZBWJBA_NUM ,
sum(case when (cycle_no = '003') then type_num else 0 end) ZGYSWJBA_NUM,
sum(case when (cycle_no = '004') then type_num else 0 end) ZBFSCB_NUM,
sum(case when (cycle_no = '005') then type_num else 0 end) HTBA_NUM,
sum(case when (cycle_no = '006') then type_num else 0 end)
from v_censorial_report
where deal_date >= TO_DATE('2007-10-01 00:00:00','YYYY-MM-DD HH24:MI:SS')
and deal_date <= TO_DATE('2007-10-31 23:59:59','YYYY-MM-DD HH24:MI:SS')
and(cycle_no = '001' or cycle_no = '002' or cycle_no = '003' or
cycle_no = '004' or cycle_no = '005' or cycle_no = '006')
到如今爲止,咱們的SQL語句已經寫出來了,如今咱們該要寫顯示頁面了,對於統計我就不詳細說了,我僅大體說一下思路:
我是把統計時間和統計人做爲HashMap的key項,把後邊的所有做爲一個Object插入HashMap的value中,原來我本打算把他們放入一個ArrayList中,可是後來發現,由於隨着統計事項(cycle_no)的改變,後邊的順序也會隨之而改變,因此我後來又選用了HashMap,這樣的話就能夠由key直接找到value了,不用擔憂位置的改變了
不過要注意查詢結果中的空值
這篇文章原本該上週發的,但是上週朋友來了,結果就沒有總結出來,如今終於出來了,就貼出來,供你們參考
固然,這裏我所採用的方法可能不是最好的,僅供你們參考,若是你們有什麼好的方法,請給我回復,謝謝!