Hourly sum of values

眉间皱痕 提交于 2021-02-16 20:22:46

问题


I have a table with the following structure and sample data:

STORE_ID |  INS_TIME  | TOTAL_AMOUNT
  2         07:46:01    20
  3         19:20:05    100
  4         12:40:21    87
  5         09:05:08    5
  6         11:30:00    12
  6         14:22:07    100

I need to get the hourly sum of TOTAL_AMOUNT for each STORE_ID. I tried the following query but i don't know if it's correct.

SELECT STORE_ID, SUM(TOTAL_AMOUNT) , HOUR(INS_TIME) as HOUR FROM VENDAS201302 
WHERE MINUTE(INS_TIME) <=59
GROUP BY HOUR,STORE_ID
ORDER BY INS_TIME;

回答1:


SELECT STORE_ID,  
       HOUR(INS_TIME) as HOUR_OF_TIME,
       SUM(TOTAL_AMOUNT) as AMOUNT_SUM
FROM VENDAS201302 
GROUP BY STORE_ID, HOUR_OF_TIME
ORDER BY INS_TIME;



回答2:


Not sure why you are not considering different days here. You could get the hourly sum using Datepart() function as below in Sql-Server:

DEMO

SELECT STORE_ID, SUM(TOTAL_AMOUNT) HOURLY_SUM
FROM t1
GROUP BY STORE_ID, datepart(hour,convert(datetime,INS_TIME))
ORDER BY STORE_ID


来源:https://stackoverflow.com/questions/15394667/hourly-sum-of-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!