SQL Group By - 将结果作为单行而不是多行返回(展平结果)

时间:2013-12-13 14:59:16

标签: sql group-by flatten

我有以下工作表:

job_id | is_full_time | is_short_term 
     1 |      0       |      0
     2 |      0       |      1
     3 |      1       |      0
     4 |      1       |      1

我有以下查询,我得到4行结果。

SELECT is_full_time, is_short_term, 
COUNT(CASE WHEN is_full_time = 1 AND is_short_term = 0 THEN 1 ELSE NULL END) AS FT_Long, 
COUNT(CASE WHEN is_full_time = 1 AND is_short_term = 1 THEN 1 ELSE NULL END) AS FT_Short,
COUNT(CASE WHEN is_full_time = 0 AND is_short_term = 0 THEN 1 ELSE NULL END) AS PT_Long, 
COUNT(CASE WHEN is_full_time = 0 AND is_short_term = 1 THEN 1 ELSE NULL END) AS PT_Short
FROM job
GROUP by is_full_time, is_short_term

我的结果如下:

FT_Long | FT_Short | PT_Long | PT_Short
    1   0     0     0
    0   1     0     0
    0   0     1     0
    0   0     0     1

如何将此结果合并到一个结果行中?

我想看到这个:

 FT_Long | FT_Short | PT_Long | PT_Short
        1   1     1     1

4 个答案:

答案 0 :(得分:5)

通常,此模板适用于以下情况:

SELECT SUM(FT_Long) as FT_Long, SUM(FT_Short) AS FT_Short, SUM(PT_Long) AS PT_Long, SUM(PT_Short) AS PT_Short
FROM
(
  SELECT is_full_time, is_short_term, 
  COUNT(CASE WHEN is_full_time = 1 AND is_short_term = 0 THEN 1 ELSE NULL END) AS FT_Long, 
  COUNT(CASE WHEN is_full_time = 1 AND is_short_term = 1 THEN 1 ELSE NULL END) AS FT_Short,
  COUNT(CASE WHEN is_full_time = 0 AND is_short_term = 0 THEN 1 ELSE NULL END) AS PT_Long, 
  COUNT(CASE WHEN is_full_time = 0 AND is_short_term = 1 THEN 1 ELSE NULL END) AS PT_Short
  FROM job
  GROUP by is_full_time, is_short_term
) T

在这种情况下,我相信您会得到与以下相同的结果,这将取决于您的数据中原始查询中需要该组的其他内容

  SELECT 
  SUM(CASE WHEN is_full_time = 1 AND is_short_term = 0 THEN 1 ELSE 0 END) AS FT_Long, 
  SUM(CASE WHEN is_full_time = 1 AND is_short_term = 1 THEN 1 ELSE 0 END) AS FT_Short,
  SUM(CASE WHEN is_full_time = 0 AND is_short_term = 0 THEN 1 ELSE 0 END) AS PT_Long, 
  SUM(CASE WHEN is_full_time = 0 AND is_short_term = 1 THEN 1 ELSE 0 END) AS PT_Short
  FROM job

答案 1 :(得分:1)

您发布的结果与您的select语句不符。具体来说,您的select语句包括is_full_time和is_short_term。如果您从选择中删除它们,则可以通过以下方式将其从组中删除。

SELECT
COUNT(CASE WHEN is_full_time = 1 AND is_short_term = 0 THEN 1 ELSE NULL END) AS FT_Long, 
COUNT(CASE WHEN is_full_time = 1 AND is_short_term = 1 THEN 1 ELSE NULL END) AS FT_Short,
COUNT(CASE WHEN is_full_time = 0 AND is_short_term = 0 THEN 1 ELSE NULL END) AS PT_Long, 
COUNT(CASE WHEN is_full_time = 0 AND is_short_term = 1 THEN 1 ELSE NULL END) AS PT_Short
FROM job

答案 2 :(得分:1)

您可以安全地删除GROUP BYSUM行而不是计算

SELECT
    SUM(CASE WHEN is_full_time = 1 AND is_short_term = 0 THEN 1 ELSE NULL END) AS FT_Long, 
    SUM(CASE WHEN is_full_time = 1 AND is_short_term = 1 THEN 1 ELSE NULL END) AS FT_Short,
    SUM(CASE WHEN is_full_time = 0 AND is_short_term = 0 THEN 1 ELSE NULL END) AS PT_Long, 
    SUM(CASE WHEN is_full_time = 0 AND is_short_term = 1 THEN 1 ELSE NULL END) AS PT_Short
FROM job

答案 3 :(得分:1)

试试这个。 SUM()可用于计算列的总和

SELECT is_full_time, is_short_term, 
SUM(COUNT(CASE WHEN is_full_time = 1 AND is_short_term = 0 THEN 1 ELSE NULL END)) AS FT_Long, 
SUM(COUNT(CASE WHEN is_full_time = 1 AND is_short_term = 1 THEN 1 ELSE NULL END)) AS FT_Short,
SUM(COUNT(CASE WHEN is_full_time = 0 AND is_short_term = 0 THEN 1 ELSE NULL END)) AS PT_Long, 
SUM(COUNT(CASE WHEN is_full_time = 0 AND is_short_term = 1 THEN 1 ELSE NULL END)) AS PT_Short
FROM job
GROUP by is_full_time, is_short_term