以下は私のスクリプトの一部です。
select a. itemcode, sum(b.quantity) 'y1qty',
isnull(null,0) as 'yr2qty', sum(c.amt) 'yr1amt'
isnull(null,0) as 'yr2amt',
from ABC a
left join DEF b on a.itemcode = b.itemcode
group by a. itemcode
UNION
select a1. itemcode, isnull(null,0) as 'yr1qty',
sum(b1.quantity) 'yr2qty', isnull(null,0) as 'yr1amt',
sum(c.amt) 'yr2amt'
from ABC a1
left join DEF b1 on a1.itemcode = b1.itemcode
group by a1.itemcode
表 A: サンプル出力 (上記のクエリに基づく)
itemcode y1qty yr2qty yr1amt yr2amt
item 001 150 0 200.00 0
item 002 0 300 0 150.00
item 003 0 50 0 100.00
item 004 20 0 150.00 0
ロジックが次のようなケースステートメントを挿入したい: if y1qty = 0 and yr2qty = 0 then blah blah.. if y1qty = 0 and y2qty > 0 then blah blah.. 'DIff'
そして、この結果を達成することを目指しています:
Table B:
itemcode y1qty yr2qty Diff yr1amt yr2amt
item 001 150 0 0 200.00 0
item 002 0 300 100 0 150.00
item 003 0 50 100 0 100.00
item 004 20 0 0 150.00 0
item 005 20 30 1000 100.00 200.00