MySql查询组按日和时间分组

时间:2014-10-10 14:19:09

标签: mysql sql

我正在尝试创建一个SQL查询来解决我的问题。

我的表:

+----+---------------------+-------+
| 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   |
+------------+-------+---------+-----------+-------+

2 个答案:

答案 0 :(得分:1)

您可以使用sumcaseSELECT 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`) 个表达式:

{{1}}

答案 1 :(得分:0)

有多种方法可以解决这个问题,但对于我自己,我首先要通过在CROSS APPLY中提取伪信息,然后对此信息进行分组来实现。

我相信这提供了显着的可读性优势,并允许您重复使用其他条款中的任何计算。例如,您已集中了分组机制,这意味着您只需要在一个位置而不是在select和group by中更改它。同样,您可以添加&#34; extraData.Morning = 1&#34;到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