-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

这可能吗?这是编辑过的。这是第二次编辑,如果可能的话?

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 回答
0

更简单的解决方案:

SELECT sale_order FROM 
(
   select partner, sale_order from tablename
   UNION
   select partner||'1',partner FROM tablename
)
AS temp
ORDER BY partner desc
于 2013-10-30T10:35:00.740 回答