jasperReport和JasperServer的使用實例

首先見第一個需求如下圖所示:

一開始是選用Case When來實現分組(這裏按季度分組)顯示,如以下測試SQL:
SELECT
(CASE 
 WHEN (ob_created_time> '2016-01-06 09:51' and ob_created_time < '2016-03-06 09:54') THEN '2016年第1季度'  
 WHEN (ob_created_time> '2016-07-06 09:51' and ob_created_time < '2016-07-07 09:53') THEN '2016年第2季度' 
 WHEN (ob_created_time> '2016-07-07 09:59' and ob_created_time < '2016-07-08 09:51') THEN '2016年第3季度' 
 WHEN (ob_created_time> '2016-09-06 09:51' and ob_created_time < '2016-12-06 09:54') THEN '2016年第4季度' 
 END) quarter,
 COUNT(ob_uuid) userCounts FROM am_object WHERE ob_object_class='User'
 GROUP BY quarter;
最後用一個union將不同分組(quarter,year,month)的查詢結果鏈接起來,測試sql如下:

 SELECT
(CASE 
 WHEN (ob_created_time> '2016-01-01 09:51' and ob_created_time < '2016-03-31 09:54') THEN '2016年第1季度'  
 WHEN (ob_created_time> '2016-04-01 09:51' and ob_created_time < '2016-06-30 09:53') THEN '2016年第2季度' 
 WHEN (ob_created_time> '2016-07-01 09:59' and ob_created_time < '2016-09-30 09:51') THEN '2016年第3季度' 
 WHEN (ob_created_time> '2016-10-01 09:51' and ob_created_time < '2016-12-31 09:54') THEN '2016年第4季度' 
 END) t,COUNT(ob_uuid) userCounts FROM am_object WHERE ob_object_class='User' and 'quarter' like 'quarter' GROUP BY t
UNION
SELECT
(CASE 
 WHEN (ob_created_time> '2015' and ob_created_time < '2016') THEN '2015年'  
 WHEN (ob_created_time> '2016' and ob_created_time < '2017') THEN '2016年' 
 WHEN (ob_created_time> '2017' and ob_created_time < '2018') THEN '2017年' 
 WHEN (ob_created_time> '2018' and ob_created_time < '2019') THEN '2018年' 
 END) t,COUNT(ob_uuid) userCounts FROM am_object WHERE ob_object_class='User' and 'year' like 'year' GROUP BY t
UNION
SELECT
(CASE 
 WHEN (ob_created_time> '2016-01' and ob_created_time < '2016-02') THEN '1月份'
 WHEN (ob_created_time> '2016-02' and ob_created_time < '2016-03') THEN '2月份'
 WHEN (ob_created_time> '2016-03' and ob_created_time < '2016-04') THEN '3月份'
 WHEN (ob_created_time> '2016-04' and ob_created_time < '2016-05') THEN '4月份'
 WHEN (ob_created_time> '2016-05' and ob_created_time < '2016-06') THEN '5月份'
 WHEN (ob_created_time> '2016-06' and ob_created_time < '2016-07') THEN '6月份' 
 WHEN (ob_created_time> '2016-07' and ob_created_time < '2016-08') THEN '7月份' 
 WHEN (ob_created_time> '2016-08' and ob_created_time < '2016-09') THEN '8月份'
 WHEN (ob_created_time> '2016-09' and ob_created_time < '2016-10') THEN '9月份'
 WHEN (ob_created_time> '2016-10' and ob_created_time < '2016-11') THEN '10月份'
 WHEN (ob_created_time> '2016-11' and ob_created_time < '2016-12') THEN '11月份'
 WHEN (ob_created_time> '2016-12' and ob_created_time < '2016-12-31') THEN '12月份'
 END) t, COUNT(ob_uuid) userCounts FROM am_object WHERE ob_object_class='User' and 'month' like 'month' GROUP BY t;
顯示結果如下:

接下來考慮如何將開始日期和結束日期作爲一個變量值傳入,首先查詢出在開始和結束日期間的所有數據,sql如下:
select * from am_object 
WHERE ob_object_class='User' 
and ob_created_time>='2016'
and ob_created_time<='2017'
然後再將查詢出來的結果根據時間進行分組,如下sql:
 SELECT
(CASE 
 WHEN (QUARTER(z.ob_created_time)=1) THEN (CONCAT(YEAR(z.ob_created_time),'年第1季度'))
 WHEN (QUARTER(z.ob_created_time)=2) THEN (CONCAT(YEAR(z.ob_created_time),'年第2季度'))
 WHEN (QUARTER(z.ob_created_time)=3) THEN (CONCAT(YEAR(z.ob_created_time),'年第3季度'))
 WHEN (QUARTER(z.ob_created_time)=4) THEN (CONCAT(YEAR(z.ob_created_time),'年第4季度'))
 END) t,COUNT(z.ob_uuid) userCounts FROM 
     (
     select * from am_object 
     WHERE ob_object_class='User' 
     and ob_created_time>='2016'
     and ob_created_time<='2017'
     )z
 where 'quarter' like 'quarter' GROUP BY t
顯示如下:

整理完sql後如下所示:

SELECT
(CASE 
 WHEN (QUARTER(z.ob_created_time)=1) THEN (CONCAT(YEAR(z.ob_created_time),'年第1季度'))
 WHEN (QUARTER(z.ob_created_time)=2) THEN (CONCAT(YEAR(z.ob_created_time),'年第2季度'))
 WHEN (QUARTER(z.ob_created_time)=3) THEN (CONCAT(YEAR(z.ob_created_time),'年第3季度'))
 WHEN (QUARTER(z.ob_created_time)=4) THEN (CONCAT(YEAR(z.ob_created_time),'年第4季度'))
 END) t,COUNT(z.ob_uuid) userCounts FROM 
     (
     select * from am_object 
     WHERE ob_object_class='User' 
     and ob_created_time>='2016'
     and ob_created_time<='2017'
     )z
 where 'quarter' like 'quarter' GROUP BY t
UNION
SELECT
(CASE 
 WHEN (YEAR(z.ob_created_time)=2014) THEN '2014年'
 WHEN (YEAR(z.ob_created_time)=2015) THEN '2015年'
 WHEN (YEAR(z.ob_created_time)=2016) THEN '2016年'
 WHEN (YEAR(z.ob_created_time)=2017) THEN '2017年'
 END) t,COUNT(z.ob_uuid) userCounts FROM 
     (
     select * from am_object 
     WHERE ob_object_class='User' 
     and ob_created_time>='2016'
     and ob_created_time<='2017'
     )z
 where 'year' like 'year' GROUP BY t 
UNION
 SELECT
(CASE 
 WHEN (MONTH(z.ob_created_time)=1) THEN '1月'
 WHEN (MONTH(z.ob_created_time)=2) THEN '2月'
 WHEN (MONTH(z.ob_created_time)=3) THEN '3月'
 WHEN (MONTH(z.ob_created_time)=4) THEN '4月'
 WHEN (MONTH(z.ob_created_time)=5) THEN '5月'
 WHEN (MONTH(z.ob_created_time)=6) THEN '6月'
 WHEN (MONTH(z.ob_created_time)=7) THEN '7月'
 WHEN (MONTH(z.ob_created_time)=8) THEN '8月'
 WHEN (MONTH(z.ob_created_time)=9) THEN '9月'
 WHEN (MONTH(z.ob_created_time)=10) THEN '10月'
 WHEN (MONTH(z.ob_created_time)=11) THEN '11月'
 WHEN (MONTH(z.ob_created_time)=12) THEN '12月'
 END) t,COUNT(z.ob_uuid) userCounts FROM 
     (
     select * from am_object 
     WHERE ob_object_class='User' 
     and ob_created_time>='2016'
     and ob_created_time<='2017'
     )z
 where 'month' like 'month' GROUP BY t

這裏考慮把
     and ob_created_time>='2016'
     and ob_created_time<='2017'  作爲時間變量($P{startDateKey},$P{endDateKey})傳入,同時把季度('quarter' like 'quarter')也作爲變量值($P{timeGroup})傳入,修改後的sql如下:
select   
(CASE 
 WHEN (QUARTER(z.ob_created_time)=1) THEN (CONCAT(YEAR(z.ob_created_time),'年第1季度'))
 WHEN (QUARTER(z.ob_created_time)=2) THEN (CONCAT(YEAR(z.ob_created_time),'年第2季度'))
 WHEN (QUARTER(z.ob_created_time)=3) THEN (CONCAT(YEAR(z.ob_created_time),'年第3季度'))
 WHEN (QUARTER(z.ob_created_time)=4) THEN (CONCAT(YEAR(z.ob_created_time),'年第4季度'))
 END) t,COUNT(z.ob_uuid) userCounts FROM 
(    select * from am_object
     WHERE ob_object_class='User'
     and ob_created_time>=$P{startDateKey}
     and ob_created_time<=$P{endDateKey}
 )a
where "quarter" like $P{timeGroup} group by t
後來,爲了簡化sql,將case when替換掉,修改sql後爲:
select
    CONCAT(YEAR(a.ob_created_time),'年',QUARTER(a.ob_created_time),'季度') t,COUNT(a.ob_uuid) userCounts
    from
( select * from am_object
     WHERE ob_object_class='User'
     and ob_created_time>=$P{startDateKey}
     and ob_created_time<=$P{endDateKey}
       )a
where "quarter" like $P{timeGroup} group by t
是不是很簡潔啦?至此,sql已經寫好,接下來需要在ireport下創建相應變量parameter,如下

其中,startDateKey作爲模糊查詢變量設置如下

另外爲了計算所有userCounts,添加了total變量,其設置如下



接着導入sql後,需另外創建field變量

接着將創建的t,userCounts變量拖動到報表detail中,如下:

做好這些後,接着就是爲$P{startDateKey},$P{endDateKey}, $P{timeGroup}創建傳入什麼值了,登陸到JasperServer上面,進入userCount
編輯頁面,添加相應的開始時間,結束時間和時間分組,如下

其中已開始時間爲例,點擊“添加輸入控制”後,進入

直接點擊下一步,一步步跟着提示走




注意這裏的變量要跟你寫的SQL 中的($P{startDateKey})命名相對應。接着創建時間分組:




注意:這裏的值(quarter,year,month,week)是指傳入你寫的sql中 $P{timeGroup}的對應字符串值。
至此創建完畢,記得點擊提交,最後查看效果



至此感覺應該全部好了,其實不然,當你點擊時間後,發現傳入sql中$P{startDateKey}的時間格式爲“月日年 時分”,而我本地數據庫中的ob_created_time字段值時間格式爲“年月日 時分秒”,爲此,我重新修改了sql,修改後的sql如下:
select
    CONCAT(YEAR(a.ob_created_time),'年',QUARTER(a.ob_created_time),'季度') t,COUNT(a.ob_uuid) userCounts
    from
    ( select * from am_object
     WHERE ob_object_class='User'
     and ob_created_time>=concat(substring($P{startDateKey},7,4),'-',substring_index($P{startDateKey},'-        ',2),substring($P{startDateKey},11,6))
     and ob_created_time<=concat(substring($P{endDateKey},7,4),'-',substring_index($P{endDateKey},'-',2),substring($P{endDateKey},11,6))
       )a
where "quarter" like $P{timeGroup} group by t
union
select
    CONCAT(YEAR(a.ob_created_time),'年') t,COUNT(a.ob_uuid) userCounts
    from
    (select * from am_object
     WHERE ob_object_class='User'
     and ob_created_time>=concat(substring($P{startDateKey},7,4),'-',substring_index($P{startDateKey},'-',2),substring($P{startDateKey},11,6))
     and ob_created_time<=concat(substring($P{endDateKey},7,4),'-',substring_index($P{endDateKey},'-',2),substring($P{endDateKey},11,6))
       )a
where "year" like $P{timeGroup} group by t
union
select
    CONCAT(YEAR(a.ob_created_time),'年',MONTH(a.ob_created_time),'月') t,COUNT(a.ob_uuid) userCounts
    from
    (select * from am_object
     WHERE ob_object_class='User'
     and ob_created_time>=concat(substring($P{startDateKey},7,4),'-',substring_index($P{startDateKey},'-',2),substring($P{startDateKey},11,6))
     and ob_created_time<=concat(substring($P{endDateKey},7,4),'-',substring_index($P{endDateKey},'-',2),substring($P{endDateKey},11,6))
       )a
where "month" like $P{timeGroup} group by t
union
select
    CONCAT(YEAR(a.ob_created_time),'年第',WEEK(a.ob_created_time),'周') t,COUNT(a.ob_uuid) userCounts
    from
    (select * from am_object
     WHERE ob_object_class='User'
     and ob_created_time>=concat(substring($P{startDateKey},7,4),'-',substring_index($P{startDateKey},'-',2),substring($P{startDateKey},11,6))
     and ob_created_time<=concat(substring($P{endDateKey},7,4),'-',substring_index($P{endDateKey},'-',2),substring($P{endDateKey},11,6))
       )a
where "week" like $P{timeGroup} group by t
這裏用到了MySQL中的substring()substring_index()兩個函數,把傳入的時間格式進行強制性轉化,最後查詢效果圖如下

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