CASE子句在SQL查询中不起作用

时间:2015-02-17 10:37:40

标签: mysql sql

我的数据库中有以下表“installation_tracking”。

Intallation Tracking Table

现在我想要的是:

  1. 每种情况下的mac_id总数
  2. 每种情况下不同mac_id的总计数
  3. 如果uninstalled_date不是'0000-00-00 00:00:00'
  4. ,则mac_id的总数
  5. 如果uninstalled_date不是'0000-00-00 00:00:00'
  6. ,则不同mac_id的总数

    整个应该按rs_id分组

    我通过以下查询尝试了这一点,但它没有用。

    SELECT count(it.mac_id) AS total_installations, count(distinct it.mac_id) AS unique_installations,
    CASE  
      WHEN it.uninstalled_date <> '0000-00-00 00:00:00' THEN count(it.mac_id)
    END as total_uninstallations,
    CASE  
      WHEN it.uninstalled_date <> '0000-00-00 00:00:00' THEN count(distinct it.mac_id)
    END as total_uninstallations
    
    FROM installation_tracking it GROUP BY it.rs_id
    

5 个答案:

答案 0 :(得分:1)

试试这个:

SELECT COUNT(it.mac_id) AS total_installations, 
       COUNT(DISTINCT it.mac_id) AS unique_installations,
       COUNT(CASE WHEN it.uninstalled_date <> '0000-00-00 00:00:00' 
             THEN it.mac_id 
             ELSE NULL
             END) as total_uninstallations,
       COUNT(DISTINCT CASE WHEN it.uninstalled_date <> '0000-00-00 00:00:00' 
                      THEN it.mac_id 
                      ELSE NULL
                      END) as unique_uninstallations
FROM installation_tracking it 
GROUP BY it.rs_id

当值与条件不匹配时返回NULL,可以防止计算无关值。

正如funkwurm所述,ELSE NULL部分是可选的,因为默认情况下CASE子句默认返回NULL。我个人觉得它们更具可读性。

答案 1 :(得分:0)

COUNT()的结果不受CASE WHEN的影响,您必须在WHERE但必须影响所有结果。如果您想要在没有WHERE的情况下有条件地计算,那么可以使用一个小技巧。你实际上做了一个SUM(),其输入将是1或0,具体取决于条件的结果:

SELECT count(it.mac_id) AS total_installations, count(distinct it.mac_id) AS unique_installations,
SUM(CASE WHEN it.uninstalled_date <> '0000-00-00 00:00:00' THEN 1 ELSE 0 END) AS total_uninstallations,
SUM(CASE WHEN it.uninstalled_date <> '0000-00-00 00:00:00' THEN 1 ELSE 0 END) AS total_uninstallations

FROM installation_tracking it GROUP BY it.rs_id

当然这样的最后一个人不会DISTINCT。我没有办法解决我头脑中的问题。可能必须是自己的查询,但希望这会有所帮助。

答案 2 :(得分:0)

试试这个:

SELECT count(it.mac_id) AS total_installations
, count(distinct it.mac_id) AS unique_installations
, SUM(CASE WHEN it.uninstalled_date <> '0000-00-00 00:00:00'
  THEN 1 ELSE 0 END) as total_uninstallations
, SUM(CASE WHEN it.uninstalled_date <> '0000-00-00 00:00:00'
  THEN 1 ELSE 0 END) as total_uninstallations

FROM installation_tracking it GROUP BY it.rs_id

答案 3 :(得分:0)

将案例/总和翻出来:

SELECT count(it.mac_id) AS total_installations,
       count(distinct it.mac_id) AS unique_installations,
       SUM(CASE WHEN it.uninstalled_date <> '0000-00-00 00:00:00' THEN 1 else 0 end) as total_uninstallations1,
       SUM(CASE WHEN it.uninstalled_date <> '0000-00-00 00:00:00' THEN 1 else 0 end) as total_uninstallations2
FROM installation_tracking it
GROUP BY it.rs_id
BTW,相同的列别名无法重复使用......

答案 4 :(得分:-1)

您的查询应如下所示:

SELECT count(it.mac_id) AS total_installations, count(distinct it.mac_id) AS unique_installations,
  CASE  
  WHEN it.uninstalled_date <> '0000-00-00 00:00:00' THEN count(it.mac_id)
  WHEN it.uninstalled_date <> '0000-00-00 00:00:00' THEN count(distinct it.mac_id) 
  END as total_uninstallations
FROM installation_tracking it GROUP BY it.rs_id