
sql语句怎么计算一个月?
比如说(表中有时间字段)到了一个月了就记录这个月的总数但是怎么样才知道到了一个月了呢?但是在插入数据的时候你是不知道在什么时候插入进去的,...
比如说(表中有时间字段) 到了一个月了 就记录这个月的 总数 但是怎么样才知道到了一个月了呢? 但是在插入数据的时候 你是不知道在什么时候插入进去的,
展开
3个回答
展开全部
先把年月取出来,分下组,求下每月的总数,然后对年月总数用where过滤一下就行了。
select year, month, Count
from (select to_char(to_date(t.ymd), 'yyyy') as year,
to_char(to_date(t.ymd), 'mm') as month,
count(*) as Count
from tablename t
group by to_char(to_date(t.ymd), 'yyyy'),
to_char(to_date(t.ymd), 'mm')
order by year, month)
where (month in (1, 3, 5, 7, 8, 10, 12) and count = 31)
or (month in (4, 6, 9, 11) and count = 30)
or (month = 2 and mod(year, 4) = 0 and count = 29)
or (month = 2 and mod(year, 4) <> 0 and count = 28)
select year, month, Count
from (select to_char(to_date(t.ymd), 'yyyy') as year,
to_char(to_date(t.ymd), 'mm') as month,
count(*) as Count
from tablename t
group by to_char(to_date(t.ymd), 'yyyy'),
to_char(to_date(t.ymd), 'mm')
order by year, month)
where (month in (1, 3, 5, 7, 8, 10, 12) and count = 31)
or (month in (4, 6, 9, 11) and count = 30)
or (month = 2 and mod(year, 4) = 0 and count = 29)
or (month = 2 and mod(year, 4) <> 0 and count = 28)
展开全部
where 时间字段 between dateadd(m,-1,getdate()) and getdate()
就是你要的一个月内的日期
就是你要的一个月内的日期
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
表中记个毛总数,总数属于统计信息,什么时候查什么时候算啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询