当前位置: 代码迷 >> Sql Server >> 按日统计总数解决方案
  详细解决方案

按日统计总数解决方案

热度:9   发布时间:2016-04-24 10:45:00.0
按日统计总数
http://e.hiphotos.bdimg.com/album/s%3D1600%3Bq%3D90/sign=86a26e190ef431adb8d2473f7b0697de/a50f4bfbfbedab64375ac64bf536afc379311e14.jpg

一张月表内有每日的若干条数据,想按日统计每日的数据总数;


2014-5-1 87 920 910
2014-5-1 99 1190 780
2014-5-1 88 1050 840
2014-5-1 99 1190 780
2014-5-2 87 920 910
2014-5-2 88 1050 840
2014-5-2 75 730 890
2014-5-2 87 740 600
2014-5-2 75 730 890
2014-5-2 87 920 910
2014-5-2 99 1190 780
2014-5-4 88 1050 840
2014-5-4 99 1190 780
2014-5-4 87 920 910
2014-5-4 88 1050 840
2014-5-4 75 730 890
2014-5-4 87 740 600
2014-5-7 75 730 890
2014-5-8 87 740 600
2014-5-8 89 1090 820
2014-5-9 68 2450 3560
2014-5-9 68 2450 3560
2014-5-9 78 3840 4350
2014-5-9 89 1090 820
2014-5-9 78 3840 4350



变成:

日期      统计
2014-5-1 4
2014-5-2 7
2014-5-4 6
2014-5-7 1
2014-5-8 2
2014-5-9 5

------解决方案--------------------

select [日期字段] '日期',
       count(1) '统计'
 from [表名]
 group by [日期字段]

------解决方案--------------------
测试过了?

if object_id('[Test]') is not null 
drop table [Test] 
go
create table Test (high_ID int, high_datetime datetime)
insert Test
select 1,'2014/5/1 0:33:07'   union all
select 2,'2014/5/8 7:23:23'   union all
select 3,'2014/5/9 10:24:27'  union all
select 4,'2014/5/10 13:14:19' union all
select 5,'2014/5/13 13:13:35' union all
select 6,'2014/5/20 11:15:15' union all
select 7,'2014/5/21 22:30:45' union all
select 8,'2014/5/24 23:10:46'
select convert(varchar,high_datetime,23) as '日期',count(1) as '统计' from test
group by convert(varchar,high_datetime,23)

日期                             统计          
------------------------------ ----------- 
2014-05-01                     1
2014-05-08                     1
2014-05-09                     1
2014-05-10                     1
2014-05-13                     1
2014-05-20                     1
2014-05-21                     1
2014-05-24                     1

(所影响的行数为 8 行)

------解决方案--------------------

select  max(high_datetime) as  '日期',
       count(1) '统计'
 from [表名]
 group by Convert(varchar, [high_datetime],23)

或者

select  high_datetime as  '日期',
       count(1) '统计'
 from [表名]
 group by high_datetime

这两个你都试试, 应该有一个合适你的
  相关解决方案