mysql在报告

时间:2018-06-15 09:56:38

标签: mysql sql case

目前,我显示的每周报告只有7列,对于给定的周日期范围。

日期范围是从jQuery日期选择器严格输入仅一周。

user   1   |   2   |   3   |   4   |   5   |   6   |   7
---------------------------------------------------------
132    0   |   0   |   0   |   1   |   1   |   0   |   1
133    0   |   1   |   1   |   1   |   0   |   0   |   0
134    1   |   1   |   1   |   0   |   1   |   0   |   0
135    1   |   0   |   1   |   1   |   0   |   0   |   0

现在我想接受任何日期范围输入(不仅仅是一周范围),并显示如下记录:

user   2018-06-03   |   2018-06-04   |   2018-06-05   |   2018-06-06   |   2018-06-07   |   2018-06-08   |   2018-06-09   |   2018-06-10   |   ...
---------------------------------------------------------------------------------------------------------------------------------------------
132   0            |       0        |       1        |       1        |       1        |       1        |       0        |       1        |
133   0            |       0        |       0        |       1        |       1        |       1        |       1        |       1        |
134   0            |       1        |       0        |       1        |       1        |       1        |       1        |       0        |
135   0            |       1        |       1        |       1        |       1        |       1        |       1        |       0        |
136   0            |       0        |       1        |       0        |       1        |       1        |       1        |       1        |

要显示7天周报告,这是我的查询脚本:

SELECT userId as user,
SUM(CASE WHEN day = 1 THEN lunchStatus ELSE 0 END) '1',
    SUM(CASE WHEN day = 2 THEN lunchStatus ELSE 0 END) '2',
    SUM(CASE WHEN day = 3 THEN lunchStatus ELSE 0 END) '3',
    SUM(CASE WHEN day = 4 THEN lunchStatus ELSE 0 END) '4',
    SUM(CASE WHEN day = 5 THEN lunchStatus ELSE 0 END) '5',
    SUM(CASE WHEN day = 6 THEN lunchStatus ELSE 0 END) '6',
    SUM(CASE WHEN day = 7 THEN lunchStatus ELSE 0 END) '7'
FROM
(
    SELECT userId, lunchStatus, DAYOFWEEK(issuedDateTime) as day 
    FROM `lunch_status` 
    WHERE DATE(issuedDateTime) BETWEEN '$week_start_date' AND '$week_end_date' 
) as a
GROUP BY user

如何更新我的查询以获得我需要的内容。

我有点被困在这里,整个日期范围的东西。

感谢任何帮助。感谢。

0 个答案:

没有答案