请教个时间段统计的问题

zbking 2011-11-25 12:47:11
ID DATETIME VAR1
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
...全文
88 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
--小F-- 2011-11-25
  • 打赏
  • 举报
回复
select
convert(varchar(10),datetime,120) ,count(1),sum(var1)
from
tb
group by
convert(varchar(10),datetime,120)
jwdream2008 2011-11-25
  • 打赏
  • 举报
回复
select convert(char(8),[DATETIME],114) date
,count()
,sum(VAR1)
from TB
group by convert(char(8),[DATETIME],114)
-晴天 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
pengxuan 2011-11-25
  • 打赏
  • 举报
回复

2楼到1楼的前面了
pengxuan 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 行受影响)
*/
  • 打赏
  • 举报
回复

select to_char(DATETIME,'yyyy-mm-dd') ,count(VAR1),sum(VAR1)
from tb1
group by to_char(DATETIME,'yyyy-mm-dd')
pengxuan 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 行受影响)

*/
dawugui 2011-11-25
  • 打赏
  • 举报
回复
select convert(varchar(10),DATETIME,120) DATETIME , count(1) cnt , sum(var1) [sum]
from tb
gruop by convert(varchar(10),DATETIME,120)
tianyazaixian 2011-11-25
  • 打赏
  • 举报
回复
我也看看 。。。
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hj_daxian 的回复:]

SQL code

select to_char(DATETIME,'yyyy-mm-dd') ,count(VAR1),sum(VAR1)
from tb1
group by to_char(DATETIME,'yyyy-mm-dd')
[/Quote]

忘了 这是sql区
to_char(DATETIME,'yyyy-mm-dd') → convert(varchar(10),datetime,120)
yhui1989love 2011-11-25
  • 打赏
  • 举报
回复
太不容易了的说!!!!!
SylarZhou 2011-11-25
  • 打赏
  • 举报
回复
唉 抢分不容易啊
SylarZhou 2011-11-25
  • 打赏
  • 举报
回复
select CONVERT(varchar(10),[Datetime],112), count(datetime) from  table  group by  CONVERT(varchar(10),[Datetime],112)  后面那个数字没看懂你是在统计啥

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧