SQL Server 2008查询 - 表连接

时间:2016-06-24 14:17:39

标签: sql sql-server sql-server-2008 tsql

我无法从SQL Server 2008查询中获得我想要的结果。查询1为[GeneralErrors]生成我想要的结果,查询2为[RehabErrors]生成正确的结果,但我确实需要它们在同一行。当我尝试在查询3中组合这两个查询时,[GeneralErrors]的结果是不正确的,但是我的表格方式也是如此。

查询1:

SELECT
        ReviewID, 
        SUM(CASE Score_CorrectID_Accuracy WHEN 'Error' THEN 1 ELSE 0 END + 
        CASE Score_ProperlyIdentified_Accuracy WHEN 'Error' THEN 1 ELSE 0 END) AS GeneralErrors
    FROM  
        Reviews
    WHERE 
        (UserID IS NOT NULL AND UserID <> '')
    GROUP BY 
        Reviews.ReviewID

Results:

ReviewID    GeneralErrors
7           0
8           0
9           0
10          0
11          0
12          9
13          0
14          0
15          4

查询2:

SELECT 
         Reviews.ReviewID
        ,COUNT(RehabMetricsCalls.ReviewID) AS RehabErrors
    FROM    RehabMetrics INNER JOIN
             RehabMetricsCalls ON RehabMetrics.RehabMetricID = RehabMetricsCalls.RehabMetricID RIGHT OUTER JOIN
             Reviews ON RehabMetricsCalls.ReviewID = Reviews.ReviewID   
    WHERE 
        (UserID IS NOT NULL AND UserID <> '')
    GROUP BY 
        Reviews.ReviewID

Results:

ReviewID    RehabErrors
7           3
8           0
9           0
10          0
11          0
12          5
13          5
14          0
15          4

查询3: 我尝试组合产生错误结果的两个查询

SELECT DISTINCT 
        Reviews.ReviewID
        ,SUM(CASE Score_CorrectID_Accuracy WHEN 'Error' THEN 1 ELSE 0 END + 
        CASE Score_ProperlyIdentified_Accuracy WHEN 'Error' THEN 1 ELSE 0 END + 
        CASE Score_MiniMiranda_Accuracy WHEN 'Error' THEN 1 ELSE 0 END + 
        CASE Score_Tone_Accuracy WHEN 'Error' THEN 1 ELSE 0 END + 
        CASE Score_Accuracy_Accuracy WHEN 'Error' THEN 1 ELSE 0 END + 
        CASE Score_Notepad_Accuracy WHEN 'Error' THEN 1 ELSE 0 END +        
        CASE Score_PCAResponsive_Accuracy WHEN 'Error' THEN 1 ELSE 0 END + 
        CASE Score_AWGInfo_Accuracy WHEN 'Error' THEN 1 ELSE 0 END + 
        CASE Complaint_Accuracy WHEN 'Error' THEN 1 ELSE 0 END) AS GeneralErrors
        ,COUNT(RehabMetricsCalls.ReviewID) AS RehabErrors
    FROM    RehabMetrics INNER JOIN
             RehabMetricsCalls ON RehabMetrics.RehabMetricID = RehabMetricsCalls.RehabMetricID INNER JOIN
             Reviews ON RehabMetricsCalls.ReviewID = Reviews.ReviewID   
    WHERE 
        (UserID IS NOT NULL AND UserID <> '')
    GROUP BY 
        Reviews.ReviewID

期望的结果:

ReviewID    GeneralErrors   RehabErrors
    7           0               3
    12          45              5
    13          0               5
    15          16              4

1 个答案:

答案 0 :(得分:0)

做一个加入..

;with Firstquery  as
(
SELECT
 ReviewID, 
 SUM(CASE Score_CorrectID_Accuracy WHEN 'Error' THEN 1 ELSE 0 END + 
CASE Score_ProperlyIdentified_Accuracy WHEN 'Error' THEN 1 ELSE 0 END) AS GeneralErrors
 FROM  
 Reviews
 WHERE 
(UserID IS NOT NULL AND UserID <> '')
 GROUP BY 
Reviews.ReviewID
)
,Secondquery as
(
SELECT 
  Reviews.ReviewID
 ,COUNT(RehabMetricsCalls.ReviewID) AS RehabErrors
FROM    RehabMetrics INNER JOIN
   RehabMetricsCalls ON RehabMetrics.RehabMetricID = RehabMetricsCalls.RehabMetricID RIGHT OUTER JOIN
 Reviews ON RehabMetricsCalls.ReviewID = Reviews.ReviewID   
 WHERE 
 (UserID IS NOT NULL AND UserID <> '')
 GROUP BY 
  Reviews.ReviewID
)
select 
fs.reviewid,fs.generalerrors,sq.rehaberrors
from
firstquery fs
join
secondquery sq
on fs.reviewid=sq.reviewid