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

必要な形式でデータを取得するためのクエリを作成できます。CASEMySQLでは、集計関数とステートメントを使用できます。

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 FiddlewithDemoを参照してください

于 2012-11-09T11:32:34.490 に答える