透过案例和小组

时间:2017-02-16 06:11:32

标签: mysql group-by case

我有一个不太优雅的解决方案,使用我之前使用的CASE函数在MySQL中转动SELECT查询,但我无法看到我可能出错的地方,因为我没有得到我期望的结果。试图传达我的意图:

我正在尝试跟踪一个唯一引用的时间戳的进度,我可以轻松地将其引入SELECT查询,但需要将它们转移到行。我用以下查询完成了这个:

SELECT c.ID, d.lead_id,
CASE WHEN s.summary LIKE '%submitted and status set to "Pending FA Approval"%' THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END AS a,
CASE WHEN s.summary LIKE '%("Pending FA Approval" => "Not Approved, Please Revise")%' THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END AS b,
CASE WHEN s.summary LIKE '%("Not Approved, Please Revise" => "Pending FA Approval")%' THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END AS c,
CASE WHEN s.summary LIKE '%("Pending FA Approval" => "Complete, Pending Payment")%' THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END AS d,
CASE WHEN s.summary LIKE '%("Complete, Pending Payment" => "Paid, Complete")%' THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END AS e,
CASE WHEN s.summary LIKE '%("Paid, Complete" => "Processing")%' THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END AS f,
CASE WHEN s.summary LIKE '%("Paid, Complete" => "Processed")%' THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END AS g
FROM lead_detail d
JOIN stream s ON s.object_id = d.lead_id
JOIN chapter c ON c.ID = d.`value`
WHERE d.field_number = 41
AND s.created >= "2017-02-15"
AND s.action = "status-change"
AND s.summary NOT LIKE "%Voided%"
AND c.ID = 549

返回以下内容:

 ID     |   lead_id |   a                           |   b       |   c       |   d                           |   e       |   f       |   g
 549    |   14512   |   February 15, 2017 [08:53 PM]|   NULL    |   NULL    |   NULL                        |   NULL    |   NULL    |   NULL
 549    |   14512   |   NULL                        |   NULL    |   NULL    |   February 15, 2017 [08:54 PM]|   NULL    |   NULL    |   NULL

我遇到的问题是,当我通过将GROUP BY c.ID附加到查询结尾进行汇总时,我得到:

 ID     |   lead_id |   a                           |   b       |   c       |   d       |   e       |   f       |   g
 549    |   14512   |   February 15, 2017 [08:53 PM]|   NULL    |   NULL    |   NULL    |   NULL    |   NULL    |   NULL

而不是我想要的:

 ID |   lead_id |   a                           |   b       |   c       |   d                           |   e       |   f       |   g
549 |   14512   |   February 15, 2017 [08:53 PM]|   NULL    |   NULL    |   February 15, 2017 [08:54 PM]|   NULL    |   NULL    |   NULL

这是一个相对样本,而我正在使用的其他示例在CASE语句a-g中偶尔会有不同的值。我看了很多来源,似乎GROUP聚合不适合使用NULL,但是我无法找到一个很好的具体示例或解释来让我得到我需要的东西:一行,结果从单独的列中的CASE语句中拉出,其中返回的值为NULL(或''),直到创建日期条目时替换为止。这也需要在select查询中完成,而不方便只是将数据重组为更合适的关系表。

此外,如果重要,则需要按照说明解析此示例,但要知道UNION也会将其加入到其他c.ID值中。

任何帮助或指导都会非常感激,即使只是指出我完全错误地解决了这个问题。

1 个答案:

答案 0 :(得分:1)

单独向当前查询添加GROUP c.ID没有任何逻辑意义,甚至不会在大多数数据库中运行,因为只要指定GROUP BY,就会告诉数据库聚合记录组。因此,您选择的每个列都必须是组本身(即,可以选择c.ID必须涉及某些聚合函数,例如MAX()SUM()

SELECT c.ID,
       d.lead_id,
       MAX(CASE WHEN s.summary LIKE '%submitted and status set to "Pending FA Approval"%'
                THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END) AS a,
       MAX(CASE WHEN s.summary LIKE '%("Pending FA Approval" => "Not Approved, Please Revise")%'
                THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END) AS b,
       MAX(CASE WHEN s.summary LIKE '%("Not Approved, Please Revise" => "Pending FA Approval")%'
                THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END) AS c,
       MAX(CASE WHEN s.summary LIKE '%("Pending FA Approval" => "Complete, Pending Payment")%'
                THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END) AS d,
       MAX(CASE WHEN s.summary LIKE '%("Complete, Pending Payment" => "Paid, Complete")%'
                THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END) AS e,
       MAX(CASE WHEN s.summary LIKE '%("Paid, Complete" => "Processing")%'
                THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END) AS f,
       MAX(CASE WHEN s.summary LIKE '%("Paid, Complete" => "Processed")%'
                THEN DATE_FORMAT(CONVERT_TZ (s.created,'+00:00','-06:00'), '%M %e, %Y [%h:%i %p]') END) AS g
FROM lead_detail d
INNER JOIN stream s
    ON s.object_id = d.lead_id
INNER JOIN chapter c
    ON c.ID = d.`value`
WHERE d.field_number = 41           AND
      s.created >= "2017-02-15"     AND
      s.action = "status-change"    AND
      s.summary NOT LIKE "%Voided%" AND
      c.ID = 549
GROUP BY c.ID

以下是显示MAX()如何使枢轴工作

的简要图表
id | col
1  | 2         <-- the max of col when id=1 is 2, because NULLs are ignored
1  | NULL
1  | NULL
2  | NULL
2  | 5         <-- the max of col when id=2 is 5, because NULLs are ignored
2  | NULL