日期:2014-05-16  浏览次数:20703 次

求:取出记录的总和要符合某个条件
用户,数量,日期
name,num,date
a,1,2012-02-01
a,1,2012-03-05
b,10,2012-02-03
a,3,2012-02-09
c,2,2012-04-05

----------------------
搜索条件是:取出: 2012-02月的用户,总数量要>=2


结果:
a,4,2012-02
b,10,2012-02




------解决方案--------------------
SQL code
select name,sum(num),format(date,'%Y-%m')
from table1
where date between '2012-02-01' and '2012-02-29'
group by name
having sum(num)>=2

------解决方案--------------------
select name,sum(num),'2012-02'
from table1
where date >'2012-02-01' and date<'2012-03-01'
group by name
having sum(num)>=2