1

私は以下のようなテーブルを持っています

Date     | institution | qty1 | qty2
1 Aug 12 | xyz         |  0   | 5
1 Aug 12 | xyz         |  0   | 17
1 Aug 12 | abc         | 12   | 0
2 Aug 12 | abc         | 33   | 0
2 Aug 12 | xyz         | 0    | 57 

以下のような出力が欲しい

Date     | ABC     | XYZ  | Total
1 Aug 12 | 12      |  22  | 34
2 Aug 12 | 33      |  57  | 90
Total    | 45      | 79   | 124

これで、最初の3列のみを表示するクエリを作成しました。最後の合計列を追加する方法がわかりません

select date, sum (case when institution = 'abc', qty1 else 0 end) as ABC,
sum(case when institution =  'xyz', qty2 else 0 end) as XYZ
group by date
with rollup
4

1 に答える 1

1

あなたは非常に近いです-私はこれがそれをするべきだと思います:

select
  date
, sum (case when institution = 'abc' then qty1 else 0 end) as ABC
, sum(case when institution =  'xyz' then qty2 else 0 end) as XYZ
, sum(qty1+qty2) as Total
from mytable
group by date
with rollup

sqlfiddleのこの例へのリンクは次のとおりです。

于 2012-08-15T20:34:43.827 に答える