有table表1 如下
方式   金额1   金额2    
1       100      200
2       150      300
3       200      400
4       250      500
5       300      600
6       350      700
7       400      800
现想得到如下表2方式   金额1   金额2    
1       100      200
2       150      300
3-5     750      1500
6-7     750      15003-5 该行就是把表1的第3行——第5行整合成一行,金额则相应的是第3行——第5行的金额和,6-7类似。求查询语句。。多谢了!

解决方案 »

  1.   

    SELECT 金额1,金额2 FROM TABLE WHERE 方式<=2
    union
    select sum(金额1),sum(金额2) from table where 方式>=3 and 方式<=5
    uniom
    select sum(金额1),sum(金额2) from table where 方式>=6 and 方式<=7
      

  2.   

    create table table1
    (
           style integer,
           p1    number,
           p2    number
    )
    --插入示例数据后执行下面语句
    create table table2
    as
    select to_char(style) as 方式,p1 as 金额1,p2 as 金额2 from table1 where style <= 2
    union
    select '3-5',sum(p1) as 金额1,sum(p2) as 金额2 from table1 where style between 3 and 5 
    union 
    select '6-7',sum(p1) as 金额1,sum(p2) as 金额2 from table1 where style between 6 and 7