将“其他”案例汇总在一起进行计数

时间:2019-02-01 18:07:57

标签: mysql sql if-statement

我正在尝试对符合某些条件的项目进行计数,并按单独条件的值进行分组。我想显示每个值0到4的计数,然后显示所有大于4的计数。

我正在尝试:

SELECT
Count(*) as "Count", CASE
WHEN TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time) = 0 THEN "0" 
WHEN TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time) = 1 THEN "1" 
WHEN TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time) = 2 THEN "2" 
WHEN TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time) = 3 THEN "3" 
WHEN TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time) = 4 THEN "4"
ELSE ">4"
END
as "Days Ahead", CASE
WHEN slot.cancel_reason_id is NULL THEN "Kept"
ELSE "not kept"
END
as Status
FROM scheduling_manualapproval
JOIN scheduling_possiblereschedule ON
scheduling_manualapproval.related_suggestion_id = scheduling_possiblereschedule.id
JOIN scheduling_appointment AS slot ON 
          scheduling_possiblereschedule.available_slot_id = slot.id
WHERE scheduling_manualapproval.debug_info = ""
AND scheduling_manualapproval.action_type = 2
AND scheduling_manualapproval.action_complete = 1
group by TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time), status

我希望这能给我:

Count - Days Ahead - Status
55    - 0          - Kept
52    - 0          - Not-Kept
140   - 1          - Kept
32    - 1          - Not-Kept
27    - 2          - Kept
19    - 2          - Not-Kept
147   - 3          - Kept
67    - 3          - Not-Kept
8     - 4          - Kept
534   - 4          - Not-Kept
90    - >4         - Kept
50    - >4         - Not-Kept

但是我得到了:

Count - Days Ahead - Status
55    - 0          - Kept
52    - 0          - Not-Kept
140   - 1          - Kept
32    - 1          - Not-Kept
27    - 2          - Kept
19    - 2          - Not-Kept
147   - 3          - Kept
67    - 3          - Not-Kept
8     - 4          - Kept
534   - 4          - Not-Kept
10    - >4         - Kept
5     - >4         - Not-Kept
20    - >4         - Kept
15    - >4         - Not-Kept
30    - >4         - Kept
25    - >4         - Not-Kept
25    - >4         - Kept
3     - >4         - Not-Kept
5     - >4         - Kept
2     - >4         - Not-Kept

> 4种情况中的所有情况都分开而不是混在一起。我如何使他们成为一体?

2 个答案:

答案 0 :(得分:0)

在MySQL中,您可以使用列别名进行聚合。所以:

按“未来天数”分组,状态

答案 1 :(得分:0)

赞:

SELECT
  Count(*) as "Count", 
  CASE
    WHEN TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time) > 4 THEN '>4' 
    ELSE TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time) 
  END as "Days Ahead", 
  CASE
    WHEN slot.cancel_reason_id is NULL THEN 'Kept'
    ELSE 'not kept'
  END as Status
FROM 
  scheduling_manualapproval
  JOIN scheduling_possiblereschedule ON scheduling_manualapproval.related_suggestion_id = scheduling_possiblereschedule.id
  JOIN scheduling_appointment AS slot ON scheduling_possiblereschedule.available_slot_id = slot.id
WHERE 
  scheduling_manualapproval.debug_info = ''
  AND scheduling_manualapproval.action_type = 2
  AND scheduling_manualapproval.action_complete = 1
GROUP BY
  CASE
    WHEN TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time) > 4 THEN '>4' 
    ELSE TIMESTAMPDIFF(DAY, scheduling_manualapproval.approved_on, slot.scheduled_date_time) 
  END, status

不要在SQL中使用字符串-使用'。尽管MySQL支持使用由”大多数其他供应商所禁止的字符串-养成使用'的习惯,使您的数据库技能具有交叉适用性

为了方便起见,我修改了您的CASE;从本质上说“如果x = 1则1 elseif x = 2然后2 elseif x = 3然后3 elseif x = 4然后4 else 4> 4”毫无意义-我们可以将其舍入为“如果X> 4然后'> 4 'else x“-即除非使用X作为输出值,否则它必须大于4

您需要按整个案件的结果而不是timestampdiff的结果进行分组。之前发生的问题是您要按时间戳记diff的值进行分组,所以如果它是5,则将得到一个5组,如果6,另一个6组,如果7,另一个组..依此类推上。如果您希望“大于4的所有内容”成为单个组,则必须使用case语句和组将值5、6、7,.. 999999等转换为单个值“> 4”通过案例的输出,而不是timestampdiff的输出。

如果很难理解,我们来看一个简单的例子:

ID       Age
A        20
B        40
B        60
C        60

SELECT ID, AVG(Age) FROM t GROUP BY ID
A    20
B    50
C    60

SELECT CASE WHEN ID = 'A' THEN 'ayy' ELSE 'zed' END, AVG(Age) FROM t GROUP BY ID
ayy  20
zed  50
zed  60

最常见的查询是标准分组。最下面的一个模糊不清。分组仍然在ID上完成,但是在完成分组之后,ID会转换:如果它是'A',它将变成'ayy',否则它将变成'zed'。允许分组,因为有3个不同的ID值,所以它生成了3个组。计算平均值后,可以转换字母。 “ zed”在输出中出现两次,因为它被重新标记为B组,并且也被重新标记为C组。当然,如果按CASE WHEN分组,则将完成转换,然后将完成该组:

SELECT CASE WHEN ID = 'A' THEN 'ayy' ELSE 'zed' END, AVG(Age) FROM t 
GROUP BY CASE WHEN ID = 'A' THEN 'ayy' ELSE 'zed' END
ayy  20
zed  53.33 --this is the (40 + 60 + 60)/3 (the B plus the other B plus the C, because they all transformed to zed before they were grouped)

Gordon指出您可以按状态别名分组,但是出于交叉技能的考虑,我主张不要这样做-我遇到的大多数其他数据库供应商不允许您按别名分组在选择中命名,他们坚持要您根据别名之前选择的值进行分组。