birt报表中设置数据集sql的问题,希望有人帮我解决这个疑问,不胜感激!

这几天使用birt做报表,至于birt做么玩的我就不想说了,刚用这个东西

我也不是很熟练,只是在用的时候发现一个问题感觉很不适用报表。

一般我们做报表的sql是这样写的:

mysql> select sum(callcost) sumcallcost,
    ->    sum(duration) sumduration,
    ->    sum(chargeclass) sumchargeclass,
    ->    sum(acrtype) sumacrtype
    ->  from
    ->  (
    ->  select  callcost, duration,chargeclass, acrtype
    ->  from acr_record
    ->  limit 1,10
    ->  ) a ;
+-------------+-------------+----------------+------------+
| sumcallcost | sumduration | sumchargeclass | sumacrtype |
+-------------+-------------+----------------+------------+
|          33 |         157 |            303 |         19 |
+-------------+-------------+----------------+------------+
1 row in set (0.00 sec)

mysql>



这就是一个统计求和,你可以把结果集(只有一条数据)

中的每个字段拿出来作为图表中的一项。比如使用柱形图:

10| ——

5| | |

0|—|——|—————————————————————

sumcallcost sumdurationsumchargeclass sumacrtype


注意我们把字段名称作为了横坐标,把字段对应的值作为y轴的值。


现在我们看看birt的报表怎么设置数据集的:



仔细研究这张图你就会发现,birt是从sql结果集里取值(结果集就是那个表格)

不管结果集有多少字段,它只需要2个字段。

这样做成一个类似于map的结构,key,value.

key作为x轴显示项,value作为对应的y轴值。

你只需要传给birtx轴找那个字段,y轴找那个字段,如上图所示。


到这里,作为一个程序员,你应该发现问题了!

什么问题呢?

就是我们必须把这样一个结果集:

sumcallcost |sumduration |sumchargeclass |sumacrtype

----------------------------------------------------------------------------------

15 | 56 | 27 | 33


转化成这样的结构:


key | value

sumcallcost | 15

sumduration | 56

sumchargeclass | 27

sumacrtype | 33


然后这个结果皆可以被birt使用,x轴传参字段key

,y轴传参字段value.

这样我们期望的图形:

10| ——

5| | |

0|—|——|—————————————————————

sumcallcost sumdurationsumchargeclass sumacrtype


才能做正常显示。


现在问题已经很明白,我们要把一个只有一条数据的结果集

做列转行处理。

仔细想想这几乎不可能实现,要把列名称放到一列,再把第一行数据放到另一列。

从图形角度看,就是把这个结果集扭转90度,然分别给心得两列加个名称key,value.

目前我市写不出来这样的sql.

不过birt只是需要2列,一列作为横坐标,一列作为纵坐标。

我们还可以有其他的结构,比如:

sumcallcost |sumduration |sumchargeclass |sumacrtype |xtitlecallcost |xtitleduration |xtitlechargeclass |xtitleacrtype

----------------------------------------------------------------------------------

15 | 56 | 27 | 33 |callcostx坐标|durationx坐标 |chargeclassx坐标 |acrtypex坐标


这样birt我只能穿对应的两列,比如xtitlecallcost sumcallcost

那图像显示就是

10| ——(值15

5| | |

0|—|——|—------------------------------------

callcostx坐标

sql这样写:

select b.sumcallcost,b.sumduration,
b.sumchargeclass,b.sumacrtype 
,case 
  when b.sumcallcost is not null then 'callcost横坐标'  
  else 'callcost横坐标'  
  end xsumcallcost 
  ,case 
  when b.sumduration is not null then 'duration横坐标'  
  else 'duration横坐标'  
  end xsumduration 
  ,case 
  when b.sumchargeclass is not null then 'chargeclass横坐标'  
  else 'chargeclass横坐标'  
  end xsumchargeclass 
  ,case 
  when b.sumacrtype is not null then 'acrtype横坐标'  
  else 'acrtype横坐标'  
  end xsumacrtype 
from 
(
	select sum(callcost) sumcallcost, 
	  sum(duration) sumduration,
	  sum(chargeclass) sumchargeclass, 
	  sum(acrtype) sumacrtype  
	from 
	(
	select  callcost, duration,chargeclass, acrtype
	from acr_record 
	limit 1,10
	) a 
) b 

mysql> select b.sumcallcost,b.sumduration,
    -> b.sumchargeclass,b.sumacrtype
    -> ,case
    ->   when b.sumcallcost is not null then 'callcost横坐标'
    ->   else 'callcost横坐标'
    ->   end xsumcallcost
    ->   ,case
    ->   when b.sumduration is not null then 'duration横坐标'
    ->   else 'duration横坐标'
    ->   end xsumduration
    ->   ,case
    ->   when b.sumchargeclass is not null then 'chargeclass横坐标'
    ->   else 'chargeclass横坐标'
    ->   end xsumchargeclass
    ->   ,case
    ->   when b.sumacrtype is not null then 'acrtype横坐标'
    ->   else 'acrtype横坐标'
    ->   end xsumacrtype
    -> from
    -> (
    ->  select sum(callcost) sumcallcost,
    ->    sum(duration) sumduration,
    ->    sum(chargeclass) sumchargeclass,
    ->    sum(acrtype) sumacrtype
    ->  from
    ->  (
    ->  select  callcost, duration,chargeclass, acrtype
    ->  from acr_record
    ->  limit 1,10
    ->  ) a
    -> ) b ;
+-------------+-------------+----------------+------------+----------------+----
------------+-------------------+---------------+
| sumcallcost | sumduration | sumchargeclass | sumacrtype | xsumcallcost   | xsu
mduration   | xsumchargeclass   | xsumacrtype   |
+-------------+-------------+----------------+------------+----------------+----
------------+-------------------+---------------+
|          33 |         157 |            303 |         19 | callcost横坐标
 | duration横坐标       | chargeclass横坐标       | acrtype横坐标       |
+-------------+-------------+----------------+------------+----------------+----
------------+-------------------+---------------+
1 row in set (0.05 sec)

mysql>

这样还是不能得到我们最开始想要的结果集。

对于birt这样的数据结构,我真的很不明白,这对于大多数情况都不适用报表。

除非你把表的所有字段放到一张表里可以查。

不知道大牛们怎么用birt做的报表。

研究了几天还是想不出解决方案,

如果让我来设计birt,应该是这样:

你传一个sql,birt自动把结果集的列名作为x轴坐标,

把结果集的第一条数据作为y轴的值。

这样设计才合理,这样的sql很好写的。

真不明白birt为什么这样设计,也许是我自己不太会用birt,

但这种数据结构想想就觉得火大!

希望大牛们给我留言,小弟虚心求教!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值