2010-09-22 57 views
1

對於SQL 2008中的以下問題,有效的方法是什麼?如何使用基於計數的列進行外連接?

前兩個輸入表,使用我需要填充第三(數據出表)

基本上,WDATA將具有相應於DATAIN表的每一行零個或多個行。 我需要用所有行填充DataOut表,包括沒有匹配和多個匹配,並填充區分WDATA中單個相應行,WDATA中沒有行或WDATA中只有一行的狀態列。

DataIn 
QID RID DOB 
------------- 
1 1 01/01/1980 
1 2 03/01/1981 
1 3 01/02/1991 



WDATA(key is QID, RID,PID) 
QID RID PID 
--------------- 
1 1 101 
1 1 102 
1 3 204 



DataOut 
QID RID PID status 
----------------------- 
1 1 101 」multiple match」 
1 1 102 」multiple match」 
1 2 null 」no match」 
1 3 204 」single match」 

回答

2

此查詢如何?

SELECT 
    di.QID, di.RID, w.PID, 
    CASE (SELECT COUNT(*) FROM WDATA w2 WHERE di.QID = w2.QID AND di.RID = w2.RID) 
     WHEN 0 THEN 'no match' 
     WHEN 1 THEN 'single match' 
     ELSE 'multiple match' 
    END AS 'Status' 
FROM 
    DataIn di 
LEFT OUTER JOIN 
    WDATA w ON di.QID = w.QID AND di.RID = w.RID 

對我來說,產生這樣的輸出:

QID RID PID Status 
1 1 101 multiple match 
1 1 102 multiple match 
1 2 NULL no match 
1 3 204 single match 

是你在找什麼?

+0

謝謝!讓我試試我的數據 – 2010-09-22 17:16:48

+0

這對我來說似乎是完美的。謝謝! – 2010-09-22 21:25:04

0

試試這個:

SELECT di.QID,di.RID,wd.PID, 
CASE 
    WHEN wd.PID is null THEN 'no match' 
    WHEN COUNT(di.QID) = 1 THEN 'single match' 
    WHEN COUNT(di.QID) > 1 THEN 'multiple match' 
END 
FROM DataIn as di 
LEFT JOIN WDATA as wd 
ON di.QID = wd.QID AND di.RID = wd.RID 
GROUP BY di.QID,di.RID,wd.PID 
+0

您錯過了「GROUP BY di.QID,di.RID,wd.PID」,但與您的朋友比我的更好:/ – Meff 2010-09-22 16:47:24

+0

看起來乾淨而緊湊,並且節省一次!現在在我的數據上檢查它。 – 2010-09-22 17:17:13

+0

在您的行GROUP BY di.QID,di.RID,wd.PID中,因爲PID分組,所以查詢不會創建多個匹配的任何記錄,因爲兩行中的PID將不同。任何想法我可以在這裏解決? – 2010-09-22 17:38:04

0

這看起來OK,我認爲它可以在雖然得到改善。

DECLARE @DataIn TABLE 
(
    QID INT NOT NULL, 
    RID INT NOT NULL, 
    DOB DATE NOT NULL 
) 
INSERT INTO @DataIn 
VALUES 
(1,1,'01/01/1980'), 
(1,2,'03/01/1981'), 
(1,3,'01/02/1991') 

DECLARE @WDATA TABLE 
(
    QID INT NOT NULL, 
    RID INT NOT NULL, 
    PID INT NOT NULL   
) 
INSERT INTO @WDATA 
VALUES 
(1,1,101), 
(1,1,102), 
(1,3,204) 

;WITH OuterCTE(QID, RID, PID) AS 
(
SELECT 
    ISNULL(D.QID, W.QID) AS QID, 
    ISNULL(D.RID, W.RID) AS RID,  
    W.PID 
FROM @DataIn AS D FULL OUTER JOIN @WDATA AS W ON W.RID = D.RID AND W.QID = D.QID 
) 

SELECT 
    CTE.QID, 
    CTE.RID, 
    CTE.PID, 
    CASE 
     WHEN COUNT(W.PID) = 0 THEN 'no match' 
     WHEN COUNT(W.PID) = 1 THEN 'single match' 
     ELSE 'multiple match' 
    END 
FROM 
    OuterCTE AS CTE 
    LEFT JOIN @WDATA AS W 
    ON CTE.QID = W.QID 
    AND CTE.RID = W.RID 
GROUP BY 
    CTE.QID, 
    CTE.RID, 
    CTE.PID