加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQLserver 按日、周、月统计数据

发布时间:2020-12-12 13:34:52 所属栏目:MsSql教程 来源:网络整理
导读:create ? table ? T(日期时间 ? datetime,? 数量 ? int)? insert ? T ? select ? ? '2007-1-1 ? 19:31:25 ',? 56? union ? all ? select ? '2007-1-1 ? 21:31:25 ',? 35? union ? all ? select ? '2007-1-1 ? 15:31:25 ',? 43? union ? all ? select ? '2007

create ? table ? T(日期时间 ? datetime,? 数量 ? int)?
insert ? T ? select ? ? '2007-1-1 ? 19:31:25 ',? 56?
union ? all ? select ? '2007-1-1 ? 21:31:25 ',? 35?
union ? all ? select ? '2007-1-1 ? 15:31:25 ',? 43?
union ? all ? select ? '2007-1-2 ? 19:31:25 ',? 34?
union ? all ? select ? '2007-1-3 ? 15:31:25 ',? 35?
union ? all ? select ? '2007-1-3 ? 21:31:25 ',? 65?
--按日统计?
select ? 日期时间=convert(char(10),? 日期时间,? 120),? 数量=sum(数量) ??
from ? T?
group ? by ? convert(char(10),? 120)?
--按周统计?
select ? 日期时间=datepart(week,? 日期时间),? 数量=sum(数量) ??
from ? T?
where ? year(日期时间)=year(getdate())?
group ? by ? datepart(week,? 日期时间)?
--按月统计?
select ? 日期时间=convert(char(7),? 数量=sum(数量) ??
from ? T?
group ? by ? convert(char(7),? 120)

实例:

按日统计

select sum(cniTicketCount) as STickCount,Convert(varchar(10),cnAppDate,23) as ScnAppDate?? from?
(select B.*,U.UserName from? SystemUsers U inner join BookingDetails B on U.AutoID=B.cnvcApplyMan
?where B.cniTicketCount>0 and? B.cnAppDate between '2010/09/01' and '2010/10/01') as A
group by Convert(varchar(10),23)

按周统计

?

按月统计

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读