带有case语句和group by的SQL聚合

时间:2016-06-19 08:46:41

标签: sql group-by case

我正在尝试使用此代码了解问题:

SELECT COUNT(CASE liveIn.state
                 WHEN ("NY" OR "NJ") THEN "group1" 
                 WHEN ("NC" or "SC") THEN "group2"
             END) AS state_groups
FROM (SELECT DISTINCT user_guid, state
        FROM users
          WHERE country="US" AND country IS NOT NULL) AS liveIn
GROUP BY state_groups; 

我得到的错误是:“无法分组'state_groups'”

我有其他代码解决了我的问题,看起来像这样(但我试图理解上面的问题):

SELECT COUNT(DISTINCT user_guid),
       CASE
           WHEN (state="NY" OR state="NJ") THEN "group1" 
           WHEN (state="NC" OR state="SC") THEN "group2"
       END AS state_groups
FROM users
     WHERE country="US" AND country IS NOT NULL
GROUP BY state_groups; 

我的输出应该如下所示:

enter image description here

TKS!

P.S.-这是课程学习课程的一部分,所以我正在研究Jupyter。

1 个答案:

答案 0 :(得分:1)

您的case when会返回组值,但它们在count中包含时无效,因为它们都会为它提供1。此外,您无法根据要根据组计算的聚合进行分组。请注意,第一个SQL中只有1列。你应该有2:一个识别组,另一个识别组。你把两者合并为一个,这没有意义。

这是一个有效的选择:

SELECT   CASE state
             WHEN "NY" THEN "group1" 
             WHEN "NJ" THEN "group1" 
             WHEN "NC" THEN "group2"
             WHEN "SC" THEN "group2"
             ELSE           "others"
         END AS state_group,
         COUNT(DISTINCT user_guid) AS user_count
FROM     USERS
WHERE    country = "US" 
GROUP BY state_group

SQL fiddle

请注意,如果在or子句的此变体中放置when,则or的第二项将单独计算,并使条件为真,因此所有值然后将在 group1 中结束。

您可以使用case when语法的其他变体,您可以使用or甚至更好,in

SELECT   CASE WHEN state IN ("NY", "NJ") THEN "group1" 
              WHEN state IN ("NC", "SC") THEN "group2"
              ELSE                            "others"
         END AS state_group,
         COUNT(DISTINCT user_guid) AS user_count
FROM     USERS
WHERE    country = "US" 
GROUP BY state_group

请注意,在此语法中,case和第一个when之间没有任何内容。