当前位置: 代码迷 >> Oracle管理 >> 求每年度最小月份的金额合计和最大月份金额合计
  详细解决方案

求每年度最小月份的金额合计和最大月份金额合计

热度:17   发布时间:2016-04-24 05:04:46.0
求每年最小月份的金额合计和最大月份金额合计
oracle怎么求每年最小月份的金额合计和最大月份金额合计。因为某些月份无销售

表结构
商品ID 销售年 销售月 金额

结果
年 最小月份的金额 最大月份金额


------解决方案--------------------
SQL code
with t as (select 销售年, 销售月, sum(金额) 月金额 from sales group by 销售年, 销售月)select 销售年, min(月金额), max(月金额) from t group by 销售年;
------解决方案--------------------
SQL code
with t as (select 销售年, min(销售月) 最小月份, max(销售月) 最大月份 from sales group by 销售年)select t.销售年, (select sum(金额) from sales where sales.销售年=t.销售年 and sales.销售月=t.最小月份) 最小月份的金额,(select sum(金额) from sales where sales.销售年=t.销售年 and sales.销售月=t.最大月份) 最大月份的金额 from t;
  相关解决方案