2016-02-03 97 views
2

我的場景:請求可能有多個(最多3個)拒絕代碼。第一個是主要拒收代碼,第二個(第二個和第三個)拒收代碼是可選的。要確定拒絕代碼排名,有一個名爲reject_rowId的字段。如果reject_rowId ='1',那麼這是主要拒絕。如果rejection_rowId ='2',那麼這是第二次拒絕。如果它是'3',那是第三。現在,我試圖在一條記錄中顯示屬於一個請求的所有reject_code值,而不是爲每個reject_code存儲一條記錄。我遇到了麻煩,並想知道是否有人可以解釋爲什麼我的SQL無法正常工作。單個字段的子查詢創建多個記錄

我的SQL看起來基本上是這樣的;

SELECT DISTINCT rea.id 
    ,(SELECT rejection_code 
     FROM rejection t1 
     INNER JOIN reason t2 ON t1.rowId = t2.rowId 
     WHERE t1.rejection_rowId = '1' 
     AND t1.rowId = REA.rowId) AS rejection1 

    ,(SELECT rejection_code 
     FROM rejection t1 
     INNER JOIN reason t2 ON t1.rowId = t2.rowId 
     WHERE t1.rejection_rowId = '2' 
     AND t1.rowId = REA.rowId) AS rejection2 

    ,(SELECT rejection_code 
     FROM rejection t1 
     INNER JOIN reason t2 ON t1.rowId = t2.rowId 
     WHERE t1.rejection_rowId = '3' 
     AND t1.rowId = REA.rowId) AS rejection3 

FROM reason rea 
INNER JOIN rejection rej ON rea.rowId = rej.rowId 
WHERE rea.id = '12345'; 

通過上面的SQL生成的輸出...

id | rejection1 | rejection2 | rejection3 
12345 | 26   | NULL  | NULL 
12345 | NULL  | 51   | NULL 
12345 | NULL  | NULL  | 3 

我試圖(和失敗)生成的輸出爲...

id | rejection1 | rejection2 | rejection3 
12345 | 26   | 51   | 3 

如果有人可以幫助我確定並解決問題,我將不勝感激。謝謝!

回答

3

我認爲你正在嘗試做這樣的事情:

SELECT 
    rea.id, 
    MAX(CASE WHEN rej.rejection_rowId = '1' THEN rej.rejection_code END) AS rejection1, 
    MAX(CASE WHEN rej.rejection_rowId = '2' THEN rej.rejection_code END) AS rejection2, 
    MAX(CASE WHEN rej.rejection_rowId = '3' THEN rej.rejection_code END) AS rejection3, 
FROM 
    reason rea 
    INNER JOIN rejection rej ON rea.rowId = rej.rowId 
WHERE 
    rea.id = '12345' 
GROUP BY rea.id 

沒有必要一次又一次同樣的表之間的加入,使用子查詢來實現你需要的是根本沒有必要的,成本很多

使用GROUP BY子句,可以將結果限制爲一行。使用集合函數內的條件時,只有在實際存在某個列時纔會在列中顯示拒絕,並且您可以將它們拆分爲單獨的列。

+0

真棒,謝謝!這個伎倆。我早些時候嘗試過使用case語句,但我沒有想到使用聚合函數和group by語句。 –

1

試試這個:

中的款項:

SELECT DISTINCT rea.id 
    ,SUM(SELECT rejection_code 
     FROM rejection t1 
     INNER JOIN reason t2 ON t1.rowId = t2.rowId 
     WHERE t1.rejection_rowId = '1' 
     AND t1.rowId = REA.rowId) AS rejection1 

    ,SUM(SELECT rejection_code 
     FROM rejection t1 
     INNER JOIN reason t2 ON t1.rowId = t2.rowId 
     WHERE t1.rejection_rowId = '2' 
     AND t1.rowId = REA.rowId) AS rejection2 

    ,SUM(SELECT rejection_code 
     FROM rejection t1 
     INNER JOIN reason t2 ON t1.rowId = t2.rowId 
     WHERE t1.rejection_rowId = '3' 
     AND t1.rowId = REA.rowId) AS rejection3 

FROM reason rea 
INNER JOIN rejection rej ON rea.rowId = rej.rowId 
WHERE rea.id = '12345' 
GROUP BY rea.id ; 

馬克斯:

SELECT DISTINCT rea.id 
    ,MAX(SELECT rejection_code 
     FROM rejection t1 
     INNER JOIN reason t2 ON t1.rowId = t2.rowId 
     WHERE t1.rejection_rowId = '1' 
     AND t1.rowId = REA.rowId) AS rejection1 

    ,MAX(SELECT rejection_code 
     FROM rejection t1 
     INNER JOIN reason t2 ON t1.rowId = t2.rowId 
     WHERE t1.rejection_rowId = '2' 
     AND t1.rowId = REA.rowId) AS rejection2 

    ,MAX(SELECT rejection_code 
     FROM rejection t1 
     INNER JOIN reason t2 ON t1.rowId = t2.rowId 
     WHERE t1.rejection_rowId = '3' 
     AND t1.rowId = REA.rowId) AS rejection3 

FROM reason rea 
INNER JOIN rejection rej ON rea.rowId = rej.rowId 
WHERE rea.id = '12345' 
GROUP BY rea.id ; 
相關問題