2013-12-13 91 views
1

我有以下工作表:SQL集團通過 - 返回結果爲單排,而不是多(拼合結果)

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 
+0

通過顯示分組的列的唯一值來進行分組。也就是說,你不是在你的聚合列上進行分組,因此你得到了他們的全部輸出。嘗試將所有列分組,看看會發生什麼。 –

+0

你正在使用哪些DBMS? Postgres的?甲骨文? –

回答

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 
+0

看起來像這樣照顧它!感謝百萬人的快速和簡單的回覆! –

1

您的發佈結果與您的選擇聲明不匹配。具體而言,您的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 
1

您可以安全地刪除對其計數

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 
+0

爲此,我看到以下錯誤:列'job.is_full_time'在選擇列表中無效,因爲它不包含在聚合函數或GROUP BY子句中 –

+0

@AdamLevitt哪個db正在使用? – giammin

1

試試這個的GROUP BYSUM行來代替。 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