请教个时间段统计的问题
时间:2011-11-25
来源:互联网
1 2011-1-1 12:10:10 1
2 2011-1-1 12:10:10 2
3 2011-1-2 12:10:10 3
4 2011-1-3 12:10:10 4
5 2011-1-3 12:10:10 5
期待如下结果:简单来讲就是日期汇总
2011-1-1 2 3
2011-1-2 1 3
2011-1-3 2 9
作者: zbking 发布时间: 2011-11-25
if object_id('tb') is not null drop table tb go create table tb ( id int identity(1,1), [datetime] datetime, var1 int ) go insert into tb([datetime],var1) select '2011-1-1 12:10:10',1 union all select '2011-1-1 12:10:10',2 union all select '2011-1-2 12:10:10',3 union all select '2011-1-3 12:10:10',4 union all select '2011-1-3 12:10:10',5 go select convert(varchar(10),[datetime],120),sum(var1) from tb group by convert(varchar(10),[datetime],120) go /* ---------- ----------- 2011-01-01 3 2011-01-02 3 2011-01-03 9 (3 行受影响) */
作者: pengxuan 发布时间: 2011-11-25
select to_char(DATETIME,'yyyy-mm-dd') ,count(VAR1),sum(VAR1) from tb1 group by to_char(DATETIME,'yyyy-mm-dd')
作者: HJ_daxian 发布时间: 2011-11-25
--改一下 if object_id('tb') is not null drop table tb go create table tb ( id int identity(1,1), [datetime] datetime, var1 int ) go insert into tb([datetime],var1) select '2011-1-1 12:10:10',1 union all select '2011-1-1 12:10:10',2 union all select '2011-1-2 12:10:10',3 union all select '2011-1-3 12:10:10',4 union all select '2011-1-3 12:10:10',5 go select [datetime]=convert(varchar(10),[datetime],120),cnt=count(*),sum(var1) from tb group by convert(varchar(10),[datetime],120) go /* datetime cnt ---------- ----------- ----------- 2011-01-01 2 3 2011-01-02 1 3 2011-01-03 2 9 (3 行受影响) */
作者: pengxuan 发布时间: 2011-11-25

2楼到1楼的前面了
作者: pengxuan 发布时间: 2011-11-25
create table tb(ID int,[DATETIME] datetime,VAR1 int) insert into tb select 1,'2011-1-1 12:10:10',1 insert into tb select 2,'2011-1-1 12:10:10',2 insert into tb select 3,'2011-1-2 12:10:10',3 insert into tb select 4,'2011-1-3 12:10:10',4 insert into tb select 5,'2011-1-3 12:10:10',5 go select CONVERT(varchar(10),[datetime],120)dt ,COUNT(*)ct,SUM(var1)s from tb group by CONVERT(varchar(10),[datetime],120) /* dt ct s ---------- ----------- ----------- 2011-01-01 2 3 2011-01-02 1 3 2011-01-03 2 9 (3 行受影响) */ go drop table tb
作者: qianjin036a 发布时间: 2011-11-25
select convert(char(8),[DATETIME],114) date ,count() ,sum(VAR1) from TB group by convert(char(8),[DATETIME],114)
作者: jwdream2008 发布时间: 2011-11-25
select convert(varchar(10),datetime,120) ,count(1),sum(var1) from tb group by convert(varchar(10),datetime,120)
作者: fredrickhu 发布时间: 2011-11-25
select CONVERT(varchar(10),[Datetime],112), count(datetime) from table group by CONVERT(varchar(10),[Datetime],112) 后面那个数字没看懂你是在统计啥
作者: SylarZhou 发布时间: 2011-11-25
作者: SylarZhou 发布时间: 2011-11-25
作者: yhui1989love 发布时间: 2011-11-25
SQL code
select to_char(DATETIME,'yyyy-mm-dd') ,count(VAR1),sum(VAR1)
from tb1
group by to_char(DATETIME,'yyyy-mm-dd')
忘了 这是sql区

to_char(DATETIME,'yyyy-mm-dd') → convert(varchar(10),datetime,120)
作者: HJ_daxian 发布时间: 2011-11-25
作者: tianyazaixian 发布时间: 2011-11-25
from tb
gruop by convert(varchar(10),DATETIME,120)
作者: dawugui 发布时间: 2011-11-25
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28