1

这是我要使用的查询

select monthNmae, monthID, achievement , target from salse

上面的查询返回以下结果

month  | monthID | achievement  | target
jan      1         12              10
feb      2         56              20
mar      3         9               20
.        .         .               .
.        .         .               .
.        .         .               .
.        .         .               .
dec     12        70               68

我想要一张来自 Jasper 报告的表格,例如

month          jan    feb    mar ............... Dec 
Achievement    12     56     9   ..............  70
target         10     26     20  ..............  68

有什么帮助吗?

4

1 回答 1

3

您可以编写查询以获取所需格式的数据。在 MySQL 中,您可以使用聚合函数和CASE语句:

select src_col as month, 
  sum(case when month = 'jan' then value end) `jan`,
  sum(case when month = 'feb' then value end) `feb`,
  sum(case when month = 'mar' then value end) `mar`,  -- add other months here
  sum(case when month = 'dec' then value end) `dec`   
from
(
  select month, monthid, achievement value, 'achievement' src_col
  from salse
  union all
  select month, monthid, target value, 'target' src_col
  from salse
) src
group by src_col

请参阅带有演示的 SQL Fiddle

于 2012-11-09T11:32:34.490 回答