2012-11-09 33 views
1

兩個單獨的類別行的報告這是我要使用想從iREPORT分享到人人交叉

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 

我想從碧玉表像

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

任何幫助嗎?

回答

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 with Demo