-1

次の表があります。金額と1つの有料フィールド

partner               sale_order   Amount     Paid             
Administrator         s0002         100       done         
Administrator         s0007         100       
Administrator         s0004         100       done
Administrator         s0009         100
Administrator         s0003         100
Demo User             s0001         200       done
Demo User             s0005         200       done
Demo User             s0008         200

私の欲望の出力は

   partner          amount       paid_amount
Administrator        500           200
    s0002            100           100 
    s0007            100 
    s0004            100           100  
    s0009            100
    s0003            100
    s0006            100
Demo User            600           400  
    s0001            200           200 
    s0008            200           200  
    s0005            200

これは可能ですか?これは編集されたものです。可能であれば、これは 2 回目の編集ですか?

4

2 に答える 2

0
with cte(partner, amount, sort, sort2) as (
    select
        partner, sum(amount), partner, 0
    from table1
    group by partner

    union all

    select
        sale_order, amount, partner, 1
    from table1
)
select data, amount
from cte
order by sort, sort2
于 2013-10-30T10:29:58.310 に答える