51代码网ORACLEMYSQLSQL SERVER其它数据库java/jspasp/asp.netC/C++/VC++APP应用其它语言服务器应用
您现在的位置: 51代码网 >> sqlserver >> 文章正文

Sql 按 时间段来 集计数据

更新时间:2012-3-10:  来源:51代码网

id createtime xiaoshoushu 
----------- ------------------------------------------------------ ----------- 
1 2012-03-09 01:01:00.000 1
2 2012-03-09 01:02:00.000 2
3 2012-03-09 02:15:00.000 3
4 2012-03-09 03:01:00.000 5
5 2012-03-09 03:15:00.000 1
6 2012-03-09 05:15:00.000 3
7 2012-03-09 05:35:00.000 4
8 2012-03-09 05:45:00.000 3

假如 我表里 数据 是这样的  
我想 取得 时间带别的 xiaoshoushu 的 集计
就是 Createtime = 2012/03/09 的 零点到 一点 , 一点到两点,两点到三点,三点到四点,四点到五点,五点到六点,六点到七点,。。。。。。。。。。。。。。。 一直到 23点 到 24点 的 分别的 销售数 
select convert(varchar(10),createtime,120) date,datepart(hh,createtime) hh,sum(xiaoshoushu) xiaoshu
from tb
where convert(varchar(10),createtime,120) = '2012-03-09'
group by convert(varchar(10),createtime,120),datepart(hh,createtime)
create table tb(id int,createtime datetime,xiaoshoushu int)
insert into tb select 1,'2012-03-09 01:01:00.000',1
insert into tb select 2,'2012-03-09 01:02:00.000',2
insert into tb select 3,'2012-03-09 02:15:00.000',3
insert into tb select 4,'2012-03-09 03:01:00.000',5
insert into tb select 5,'2012-03-09 03:15:00.000',1
insert into tb select 6,'2012-03-09 05:15:00.000',3
insert into tb select 7,'2012-03-09 05:35:00.000',4
insert into tb select 8,'2012-03-09 05:45:00.000',3
go
select convert(varchar(13),createtime,120),sum(xiaoshoushu)
from tb
group by convert(varchar(13),createtime,120)

  • 上一篇文章:
  • 下一篇文章: 没有了
  • 赞助商链接
    推荐文章
  • 此栏目下没有推荐文章
  • {
    设为首页 | 加入收藏 | 友情链接 | 网站地图 | 联系站长 |