现有表字段、值为(举例)
ID    数量   金额
1      1      1
1      2      1
2      3      1
2      4      1
2      5      1
——————————————————————
最后的查询结果为:
ID      金额      数量
1        2          1      2
2        3          3      4      5也就是说按ID分组,然后统计金额之和,数量的明细。
SQL语句应该如何写,用的ORACLE9I
请高手赐教,谢谢!

解决方案 »

  1.   

    create table T_20(
    id   Number,
    qty  Number,
    money Number
    )-------------------------------------------------------------------
    select t.id,
    t.money,
    (select max(sys_connect_by_path(id,'/')) result from
    (select id, qty,rn,lead(rn) over(partition by id order by rn) rn1
    from (select id, qty, row_number() over(order by id, qty desc) rn
    from t_20 )
    )
    start with id = t.id and rn1 is null connect by rn1 = prior rn
    ) value
    from (select distinct id,  sum(money) as money from t_20 group by ID) t