2
是否可以將case語句中的每個WHEN
輸出到其自己的列中?我嘗試過但沒有運氣。最接近我已經達到我想要的是以下。將SQL case語句顯示爲不同的列
我現在有一個查詢,如下所示:
select BH.BATCH as batch_number,
BS.TBS_DESCRIPTION as batch_description,
BH.NUM_VOUCHERS as "NUM_VOUCHERS(BATCH)",
case when VS.TVS_CODE = 1 then count(*) else 0 end as in_stock,
case when VS.TVS_CODE = 3 then count(*) else 0 end as terminate,
case when VS.TVS_CODE = 5 then count(*) else 0 end as in_progress,
case when VS.TVS_CODE = 6 then count(*) else 0 end as used,
case when VS.TVS_CODE = 8 then count(*) else 0 end as deactivate
from BATCH_HIST bh,
BATCH_STATUS bs,
VOUCH_HIST vh,
VOUCH_STATUS vs
where substr(BH.ALLOCAT_DATE, 1, 6) = 201207
and to_char(BH.STATUS) = BS.TBS_CODE
and BH.BATCH = VH.BATCH
and to_char(VH.STATUS) = VS.TVS_CODE
group by BH.BATCH,
BS.TBS_DESCRIPTION,
BH.NUM_VOUCHERS,
VS.TVS_CODE
order by 1,2,3;
這給了我下面的輸出,每個STATUS
爲不同的記錄:
BATCH_NUMBER | BATCH_DESCRIPTION | NUM_VOUCHERS(BATCH) | IN_STOCK | TERMINATE | IN_PROGRESS | USED | DEACTIVATE
------------------------------------------------------------------------------------------------------------------------------
4413565 | Allocate | 100 | 67 | 0 | 0 | 0 | 0
4413565 | Allocate | 100 | 0 | 0 | 0 | 33 | 0
4413566 | Allocate | 100 | 63 | 0 | 0 | 0 | 0
4413566 | Allocate | 100 | 0 | 0 | 0 | 37 | 0
我想這個組合在一起,以便有是每個BATCH_NUMBER
的一個記錄:
BATCH_NUMBER | BATCH_DESCRIPTION | NUM_VOUCHERS(BATCH) | IN_STOCK | TERMINATE | IN_PROGRESS | USED | DEACTIVATE
------------------------------------------------------------------------------------------------------------------------------
4413565 | Allocate | 100 | 67 | 0 | 0 | 33 | 0
4413566 | Allocate | 100 | 63 | 0 | 0 | 37 | 0