按小时统计的语句

按小时统计的语句

表:issue 字段:
id:1
time:2012-04-01 08:15:00
count:12

。。。。。

按小时统计count总数,结果如下
1 2012-04-01 8:00~9:00 134
2 2012-04-01 9:00~10:00 134

------解决方案--------------------
如果结果前面的数字是你要的计数的话,那自己另加一个计数列就是了:

SQL code
mysql> select * from issue;
+----+---------------------+-------+
| id | time                | count |
+----+---------------------+-------+
|  1 | 2012-04-01 08:15:00 |    12 |
|  2 | 2012-04-01 09:15:00 |    12 |
|  3 | 2012-04-01 08:25:00 |    12 |
|  4 | 2012-04-01 10:15:00 |    12 |
|  5 | 2012-04-01 09:15:00 |    12 |
+----+---------------------+-------+
5 rows in set (0.00 sec)

mysql> select concat(date_format(time, "%Y-%m-%d %k:00~"),  hour(time)+1, ":00")
 as ‘time‘, sum(`count`) as ‘count‘
    -> from issue
    -> group by left(time, 13);
+------------------------+-------+
| time                   | count |
+------------------------+-------+
| 2012-04-01 8:00~9:00   |    24 |
| 2012-04-01 9:00~10:00  |    24 |
| 2012-04-01 10:00~11:00 |    12 |
+------------------------+-------+
3 rows in set (0.00 sec)
------解决方案--------------------恩   group by left(time, 13);
------解决方案--------------------
select DATE_FORMAT(time,‘%Y-%m-%d %H:00‘),sum(count)from issuegroup by DATE_FORMAT(time,‘%Y-%m-%d %H:00‘)
------解决方案--------------------select Date_format(time,‘%Y-%M-%d %H:00‘),SUM(COUNT)FROM issuegroup byy DATA_FORMAR(time,‘%Y-%m-%d %h:00)
时间: 2024-10-13 22:49:29

按小时统计的语句的相关文章

统计sql语句执行效率

--统计sql语句执行效率SELECT (total_elapsed_time / execution_count)/1000 N'平均时间ms' ,total_elapsed_time/1000 N'总花费时间ms' ,total_worker_time/1000 N'所用的CPU总时间ms' ,total_physical_reads N'物理读取总次数' ,total_logical_reads/execution_count N'每次逻辑读次数' ,total_logical_reads

统计SQL语句耗时百分比

-- sql语句耗时百分比 declare @tmptb table(id int,name varchar(50),total_worker_time bigint,rate varchar(50),execute_count bigint) ;with cte1 as(select a.*,t.*from sys.dm_exec_query_stats across apply sys.dm_exec_sql_text(a.plan_handle) twhere t.dbid >= 5 ),

sqlserver 统计sql语句大全收藏

SQL统计大全收藏,主要是一些实现统计功能常用的代码,希望对需要的朋友有所帮助. 1.计算每个人的总成绩并排名 select name,sum(score) as allscore from stuscore group by name order by allscore 2.计算每个人的总成绩并排名 select distinct t1.name,t1.stuid,t2.allscore from stuscore t1,( select stuid,sum(score) as allscor

学生各门课程成绩统计SQL语句大全

学生成绩表(stuscore): 姓名:name 课程:subject 分数:score 学号:stuid 张三 数学 89 1 张三 语文 80 1 张三 英语 70 1 李四 数学 90 2 李四 语文 70 2 李四 英语 80 2 创建表 SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[stuscore]( [name] [varchar](50) COLLATE

SQLServer 统计查询语句消耗时间

--方法1[set statistic ]: set statistics time on go --执行语句 xxxx go set statistics time off --方法2[getDate()]: DECLARE @begin dateTime DECLARE @end dateTime SET @begin=getdate(); BEGIN --执行语句 xxxx end set @end=getdate(); SELECT datediff(ms,@begin,@end) as

【MySQl】按所有,今日,本月,24小时每一小时统计

-- 24小时SELECT HOUR (create_date) AS hours, SUM(source_amount) sm_source_amount, SUM(ticket_count*purchase_price) AS sm_purchase_amount FROM ticket_order WHERE DATE_FORMAT(create_date, '%Y-%m-%d') = '2017-05-12' AND scenic_id in(1,2,3); GROUP BY HOUR

sql按半小时统计

floor(4.4)  结果为4,浮点转整型 (DATE_FORMAT(PrtTime,'%H')     取小时h DATE_FORMAT(PrtTime,'%i') 取分钟m (h*60 + m) /30 取整数部分进行分类汇总 Select result,count(result) as resultTotal,floor((DATE_FORMAT(PrtTime,'%H') *60 + DATE_FORMAT(PrtTime,'%i'))/30) as groupHalfHour fro

Sql Server统计查询语句消耗时间

方法1[set statistic ]: set statistics time ongo xxxxgoset statistics time off 方法2[getDate()]: DECLARE @begin dateTimeDECLARE @end dateTimeSET @begin=getdate();BEGIN xxxxendset @end=getdate();SELECT datediff(ms,@begin,@end) as 'Elapsed Time' 方法3[设置Query

oracle 资源统计SQL语句

SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M)", D.TOT_GROOTTE_MB - F.TOTAL_BYTES "已使用空间(M)", TO_CHAR(ROUND((D.TOT_GROOTTE_MB - F.TOTAL_BYTES) / D.TOT_GROOTTE_MB * 100,2),'990.99') || '%' "使用比",