2014-10-10 140 views
2

我想創建一個SQL查詢來解決我的問題。
MySql查詢組按天和按時間

我的表:

+----+---------------------+-------+ 
| id |   date  | value | 
+----+---------------------+-------+ 
| 1 | 2014-10-10 05:10:10 | 10 | 
+----+---------------------+-------+ 
| 2 | 2014-10-10 09:10:10 | 20 | 
+----+---------------------+-------+ 
| 3 | 2014-10-10 15:10:10 | 30 | 
+----+---------------------+-------+ 
| 4 | 2014-10-10 23:10:10 | 40 | 
+----+---------------------+-------+ 
| 5 | 2014-10-11 08:10:10 | 15 | 
+----+---------------------+-------+ 
| 6 | 2014-10-11 09:10:10 | 25 | 
+----+---------------------+-------+ 
| 7 | 2014-10-11 10:10:10 | 30 | 
+----+---------------------+-------+ 
| 8 | 2014-10-11 23:10:10 | 40 | 
+----+---------------------+-------+ 

我想天總結在組值,這天三個亞組類似「早晨」(06:00 - 12:00),「午」( 12:00 - 18:00)和'晚上'(00:00 - 06:00和18:00 - 24:00)。
是這樣的:

+------------+-------+---------+-----------+-------+ 
| date | value | morning | afternoon | night | 
+------------+-------+---------+-----------+-------+ 
| 2014-10-10 | 100 | 20 |  30 | 50 | 
+------------+-------+---------+-----------+-------+ 
| 2014-10-11 | 110 | 70 |  0  | 40 | 
+------------+-------+---------+-----------+-------+ 

回答

1

你可以用一對夫婦的sum結束了case表達式:

SELECT DAY(`date`) AS `date` 
     SUM(CASE WHEN HOUR(`date`) BETWEEN 6 AND 12 THEN value ELSE 0 END) AS `morning`, 
     SUM(CASE WHEN HOUR(`date`) BETWEEN 12 AND 18 THEN value ELSE 0 END) AS `afternoon`, 
     SUM(CASE WHEN HOUR(`date`) < 6 OR HOUR(`date`) > 18 THEN value ELSE 0 END) AS `evening` 
FROM  my_table 
GROUP BY DAY(`date`) 
+0

非常感謝您!它工作得很好。 – 2014-10-10 17:17:28

0

有多種方法去了解這一點,但我自己我會做通過首先提取CROSS APPLY中的僞信息,然後將該信息分組。

我相信這提供了重要的可讀性好處,並允許您在其他子句中重新使用任何計算。例如,你已經集中了分組機制,這意味着你只需要在一個地方而不是在select和group by中進行更改。同樣,您可以將「extraData.Morning = 1」添加到WHERE子句中,而不是重新編寫早上的計算。

例如:

CREATE TABLE #TestData (ID INT, Data DATETIME, Value INT) 

INSERT INTO #TestData (ID, Data, Value) VALUES 
    (1 ,'2014-10-10 05:10:10' ,10) 
    ,(2 ,'2014-10-10 09:10:10' ,20) 
    ,(3 ,'2014-10-10 15:10:10' ,30) 
    ,(4 ,'2014-10-10 23:10:10' ,40) 
    ,(5 ,'2014-10-11 08:10:10' ,15) 
    ,(6 ,'2014-10-11 09:10:10' ,25) 
    ,(7 ,'2014-10-11 10:10:10' ,30) 
    ,(8 ,'2014-10-11 23:10:10' ,40) 

SELECT 
    extraData.DayComponent 
    ,SUM(td.Value) 
    ,SUM(CASE WHEN extraData.Morning = 1 THEN td.Value ELSE 0 END) AS Morning 
    ,SUM(CASE WHEN extraData.Afternoon = 1 THEN td.Value ELSE 0 END) AS Afternoon 
    ,SUM(CASE WHEN extraData.Night = 1 THEN td.Value ELSE 0 END) AS Night 
FROM #TestData td 
    CROSS APPLY (
     SELECT 
      DATEADD(dd, 0, DATEDIFF(dd, 0, td.Data))  AS DayComponent 
      ,CASE WHEN DATEPART(HOUR, td.Data) BETWEEN 6 AND 12 THEN 1 ELSE 0 END AS Morning 
      ,CASE WHEN DATEPART(HOUR, td.Data) BETWEEN 12 AND 18 THEN 1 ELSE 0 END AS Afternoon 
      ,CASE WHEN DATEPART(HOUR, td.Data) BETWEEN 0 AND 6 
       OR DATEPART(HOUR, td.Data) BETWEEN 18 AND 24 THEN 1 ELSE 0 END AS Night 
    ) extraData 
GROUP BY 
    extraData.DayComponent 

DROP TABLE #TestData 
+0

交叉應用是SQL Server的唯一不是嗎? – 2014-10-10 16:43:46

+0

啊,我的道歉,沒有注意到mySQL標籤。應該採取表格格式的提示。 – mrmillsy 2014-10-11 00:21:00