现在有三列名称     月份      数量 
------------------------
茄子      2          10
茄子      1           3
茄子      3           5
茄子      5          11
西红柿    1          15
西红柿    4           5
西红柿    5           6名称     时间跨度1总量(1-3)    时间跨度二 总量(4-6)
---------------------------------------------------
茄子          18                       11
西红柿        15                       11
琢磨半天 不知道用单纯的sql语句是否能实现。
想到了(case when) 不过里头似乎无法再嵌套select语句谢谢大家

解决方案 »

  1.   

    select 名称,sum(case when 月份>=1 and 月份<=3 then 数量 else 0) 时间跨度1总量(1-3) 
                sum(case when 月份>=4 and 月份<=6 then 数量 else 0) 时间跨度1总量(4-6) 
      from 表名
     group by 名称
    case when 里面不能套 可以想想外面嘛
      

  2.   

    select 名称,
      sum(case when 月份>=1 and 月份<=3 then 数量 end)时间跨度1总量(1-3) ,
      sum(case when 月份>=4 and 月份<=6 then 数量 end)时间跨度1总量(4-6) ,
    from table1 
    group by 名称
      

  3.   

    select distinct "名称",
    sum(decode(sign("月份"-3),1,0,"数量")) over(partition by "名称") "时间跨度1总量(1-3)",
    sum(decode(sign("月份"-6),1,0,decode(sign("月份"-4),-1,0,"数量"))) over(partition by "名称") "时间跨度2总量(4-6)"
    from table_name;