开发者

Extract selective records

I have a table like this:

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

I wish to split the Amount into 2 columns, namely Sales_Amt & Misc. The Region is not required. The result that I wish to have should look like this:

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 

Thanks.


That's pretty simple:

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜