GROUP BY与CASE声明

时间:2017-03-08 07:43:29

标签: mysql sql

我有一个具有以下结构的表作业

================================================
| userID  |   jobDate  |  cityID  |  totalCost |
================================================
| 1234    | 2016-04-01 |    1     |     200    |
| 1234    | 2016-04-21 |    1     |     800    |
| 1234    | 2016-08-03 |    1     |     2000   |
| 1234    | 2016-12-01 |    1     |     300    |
| 1234    | 2017-02-01 |    1     |     500    |
================================================

我希望将月份转换为列。

预期输出:

========================================================
| userID | April 2016 | May 2016 | .... | January 2017 |
========================================================
|  1234  |    1000    |   0      | .... |     500      |
========================================================

我的查询:

 SELECT u.userID, u.userFullName, ci.cityName,
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'April 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'April 2016 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'April 2016' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'April 2016 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'May 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'May 2016 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'May 2016' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'May 2016 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'June 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'June 2016 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'June 2016' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'June 2016 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'July 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'July 2016 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'July 2016' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'July 2016 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'August 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'August 2016 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'August 2016' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'August 2016 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'September 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'September 2016 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'September 2016' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'September 2016 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'October 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'October 2016 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'October 2016' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'October 2016 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'November 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'November 2016 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'November 2016' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'November 2016 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'December 2016' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'December 2016 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'December 2016' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'December 2016 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'January 2017' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'January 2017 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'January 2017' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'January 2017 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'February 2017' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'February 2017 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'February 2017' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'February 2017 Spends',

    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'March 2017' THEN GROUP_CONCAT(CONCAT(c.categoryName, ' ', s.supercategoryName))
        ELSE '-'
    END AS 'March 2017 Services',
    CASE
        WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'March 2017' THEN SUM(j.totalCost + j.discountReceived)
        ELSE 0
    END AS 'March 2017 Spends'
FROM jobs j
LEFT JOIN categories c ON c.categoryID = j.categoryID
LEFT JOIN supercategories s ON s.supercategoryID = c.supercategoryID
LEFT JOIN users u ON u.userID = j.userID
LEFT JOIN city ci ON ci.cityID = j.cityID
WHERE j.jobStatus = 'completed' AND
      DATE(j.jobDate) BETWEEN '2016-04-01' AND DATE(NOW())
GROUP BY u.userID

查询输出:

========================================================
| userID | April 2016 | May 2016 | .... | January 2017 |
========================================================
|  1234  |    1500    |   0      | .... |      0       |
========================================================

所有行都会转换到第一个匹配列中。如果我添加GROUP BY MONTHNAME(jobDate),那么每个用户会获得多行。你能帮助解决这里的错误吗?

1 个答案:

答案 0 :(得分:1)

你只是忘记了对案件的总结,看到前两个月, 你必须存入总和,因为你每月有多个记录(你可以看到你在2016年4月有两行

     SELECT u.userID, u.userFullName, ci.cityName,
       GROUP_CONCAT(CASE WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'April 2016' THEN CONCAT(c.categoryName, ' ', s.supercategoryName)
        END) AS 'April 2016 Services',
        SUM(CASE WHEN CONCAT(MONTHNAME(j.jobDate), ' ', YEAR(j.jobDate)) = 'April 2016' THEN ROUND(j.totalCost + j.discountReceived)
            ELSE 0
        END) AS 'April 2016 Spends',
    FROM jobs j
    LEFT JOIN categories c ON c.categoryID = j.categoryID
    LEFT JOIN supercategories s ON s.supercategoryID = c.supercategoryID
    LEFT JOIN users u ON u.userID = j.userID
    LEFT JOIN city ci ON ci.cityID = j.cityID
    WHERE j.jobStatus = 'completed' AND
          DATE(j.jobDate) BETWEEN '2016-04-01' AND DATE(NOW())
    GROUP BY u.userID