Dynamic grouping to achieve the cross table in Birt

source:

  https://forums.opentext.com/forums/discussion/173783/dynamic-grouping-of-crosstab-in-birt#latest

 

  In completing the business, sometimes we need to be based on the time period of dynamic cross-packet statistics, based on the length of time to achieve a daily, weekly, monthly, yearly dynamic grouping. For example: a company based on business needs, the need to follow two parameters (start date, end date) within a period of time to achieve statistical orders. Statistical rules are as follows:

  Example: The input parameter values, calculating the difference between the dates.

  var diff = End Date - Number of Days spaced ---- start date
  if (diff <15)
    the "set date" is displayed as date packets
  if (diff> 15)
    the "set date" is displayed as the circumferential packets
  if (diff > 30)
    the "set date" grouped to appear as a month
  if (diff> 365)
    will be "set date" for the group to display the year

 

  Small partners are not a bit overwhelmed by it? The above problem is essentially a problem of data preparation, but SQL or scripted data sources are difficult to write code that way, heavy workload; took the second way to use the report to hide the column, not universal and very awkward. So, a better solution is to introduce set count in the reporting tool to solve all type of problem will be easy. Here we have to Birt reporting tools, for example, tell us about the implementation process. For other reporting tools are similar.

 

  In this case, according to the input parameters, statistics from enterprises 2012-07-04 2014-05-06 start to the end of the total number of orders, total freight charges, total amount of orders this time period. Data Sheet "ORDERS" raw data as follows:

  undefined

 

  We solve this problem solvers direct view sets SPL Code:


A B C
1 =connect("demo") //Connect to the database
2 =A1.query("select   ORDERID,ORDERDATE,FREIGHT,ORDERAMOUNT from ORDERS where ORDERDATE >=? and   ORDERDATE <=?",startDate,endDate) // check out the statistics date from the beginning to the end of the ORDERS table order data between date, startDate and endDate date parameters.
3 =interval(startDate,endDate) // calculate the number of days between the start date and end date
4 if A3>365 // If the interval is greater than the number of days, 365 days a year press packet
5
=startDate|A3.(elapse@y(startDate,~)) // calculate the packet of sequence tables by year
6
=A2.group(B5.pseg(ORDERDATE);~.count(ORDERID):TotalOrder,round(~.sum(FREIGHT),2):TotalFreight,round(~.sum(ORDERAMOUNT),2):TotalOrderAmount,B5(#):BeginDate) // Press B5 range of A2 group, the total number of orders for the statistics, the total number of freight charges, total order amount, to two decimal places, and B5 as the last one.
7
= B6.new (Start Date: Start Date, # 2: Total Order, # 3: TotalFreight, # 4: TotalOrderAmount) // remove the data sequence needed to generate a new sequence tables results
8
>A1.close() // close the database
9
return B7 // return result sets grouped by year
10 else if A3>30 // If the interval is greater than the number of days 30 days 365 days & less, press packet month
11
=startDate|A3.(elapse@m(startDate,~)) // calculate the packet of sequence tables by month
12
=A2.group(B11.pseg(ORDERDATE);~.count(ORDERID):TotalOrder,round(~.sum(FREIGHT),2):TotalFreight,round(~.sum(ORDERAMOUNT),2):TotalOrderAmount,B11(#):BeginDate) //按 B11 区间对 A2 分组,统计出订单总数,运货费总数,订单金额总数,保留两位小数,并将 B11 作为最后一列。
13
=B12.new(BeginDate:BeginDate,#2:TotalOrder,#3:TotalFreight,#4:TotalOrderAmount)
14
>A1.close()
15
return B13 //返回按月份分组的结果集
16 else if A3>15 //如果天数间隔大于 15 天 & 小于等于 30 天,则按星期分组
17
=startDate|A3.(elapse(startDate,7*~)) //计算出按星期的分组序表
18
=A2.group(B17.pseg(ORDERDATE);~.count(ORDERID):TotalOrder,round(~.sum(FREIGHT),2):TotalFreight,round(~.sum(ORDERAMOUNT),2):TotalOrderAmount,B17(#):BeginDate) /按 B17 区间对 A2 分组,统计出订单总数,运货费总数,订单金额总数,保留两位小数,并将 B17 作为最后一列。
19
=B18.new(BeginDate:BeginDate,#2:TotalOrder,#3:TotalFreight,#4:TotalOrderAmount)
20
>A1.close()
21
return B19 //返回按星期分组的结果集
22 else //如果天数间隔小于 15 天,则按日期分组
23
=startDate|A3.(elapse(startDate,~)) //计算出按日期的分组序表
24
=A2.group(B23.pseg(ORDERDATE);~.count(ORDERID):TotalOrder,round(~.sum(FREIGHT),2):TotalFreight,round(~.sum(ORDERAMOUNT),2):TotalOrderAmount,B23(#):BeginDate) / Press B23 range of group A2, the total number of orders for the statistics, the total number of freight charges, total order amount, to two decimal places, and B23 as the last one.
25
= B24.new (Start Date: Start Date, # 2: Total Order, # 3: TotalFreight, # 4: TotalOrderAmount)
26
>A1.close()
27
return B25 // return result sets grouped by date

 

  The set of code resolver SPL order.dfx saved as a file, and then introduced into a Birt report. How to see cited article dry College " BIRT script calls the SPL ."

 

  BIRT report design in the following table:

  undefined

 

  Report calls set solver method and call stored procedures exactly the same, such as data stored in the BIRT centralized procedure can call orders (?,?) To call.

  Next, we look at when WEB preview, preview the results of different input parameters:

  (1) Input parameters: 2012-07-04 start time, end time intervals of several days 2014-05-06 greater than 365, the year displayed in the packet.

  undefined

  undefined

 

  (2) Input parameters: 2012-07-15 start time, end time intervals of several days 2012-12-03 greater than 30, the month displayed in the packet.

  undefined

  undefined

 

  (3) Input parameters: 2012-07-20 start time, end time is greater than the number of days 2012-08-15 15, displayed in the group week.

  undefined

  undefined

 

  (4) Input parameters: 2012-07-04 start time, end time intervals of several days 2012-07-18 less than 15, the date displayed in the packet.

  undefined

  undefined



Guess you like

Origin blog.51cto.com/12749034/2442579
Recommended