表中有 三列 年,月,总数例如年      月    总数
2011    1     12
2009    3     14
2010    5     2
2011    6     22
2011    10    15
2010    12    3如何通过sql 将2011年9月之前的所有 总数 求和?
select sum(总数)  from 表  where 年<2011  and 月<=9
上边的sql 是错的  这样的话 2010  12 的数据是取不到的。

解决方案 »

  1.   

    将2011年9月之前的所有 总数 求和
    select sum(总数) from 表 where 年<2011 and 月<=9将年月拼凑为正常日期格式select sum(总数) from 
    (select to_date(年||月,'yyyy-mm')as 日期, 总数 from 表)
     where 日期<= 2011-09
      

  2.   

    忽然发现这样 可以   where (年=2011 and 月<9) or 年<2011
      

  3.   

    select sum(总数) from  
    (select to_date(年||月,'yyyy-mm')as 日期, 总数 from 表)
     where 日期<= to_date(年||月,'2011-09')
      

  4.   

    字符串拼接,然后用to_date()进行比较
      

  5.   

    不推荐在条件中对字段进行处理,如果你的数据库设计的时候是按照年月分开的,那查询的时候最好也分开select sum(总数) from table
    where 年 < 2011 or (年=2011 and 月< 10)