提取选择性记录

时间:2011-05-16 09:51:35

标签: sql db2

我有一张这样的表:

Table A             
Customer    InvoiceNo       Region  Type    Amount
A001        10001           Europe  FG      100
B001        10002           Asia    FG      200
C001        10003           America MISC    50
D001        10004           Asia    FG      300
A001        10005           Europe  MISC    20
C001        10006           America MISC    10
B001        10007           Asia    FG      300

我希望将Amount分为两列,即Sales_Amt& MiscRegion不是必需的。 我希望得到的结果应该是这样的:

Customer    InvoiceNo       Type    Sales_Amt MISC
A001        10001           FG      100       0
B001        10002           FG      200       0
C001        10003           MISC    0         50
D001        10004           FG      300       0
A001        10005           MISC    0         20
C001        10006           MISC    0         10
B001        10007           FG      300       0 

感谢。

2 个答案:

答案 0 :(得分:2)

这非常简单:

select
    Customer, 
    InvoiceNo, 
    Type, 
    decode(Type, 'MISC', 0, 1) * Amount as Sales_Amt, 
    decode(Type, 'MISC', 1, 0) * Amount as Misc
from
    TableA
;

答案 1 :(得分:0)

Select Customer,InvoiceNo,Type,
        Case when TYPE!='MISC' Then amount Else 0 End as Sales_Amt,
        Case when TYPE='MISC' Then amount Else 0 End as Misc
From TabalA