2014-08-28 38 views
0

我想計算特定支持選項的行總數,其中行來自兩個表,如CSPLineItem和SupportOption 。我使用下面的查詢錯誤:在sql服務器中預期條件的上下文中指定的非布爾類型的表達式

SELECT DISTINCT sot.SupportOptionType_ID, 
    (Classic.NumberOfLines + MEN.NumberOfLines) TotalNumberOfLines 
FROM SupportOptionType sot WITH (NOLOCK) 
LEFT JOIN (
    SELECT sot.SupportOptionType_ID, 
     COUNT(li.CSPLineItem_ID) NumberOfLines 
    FROM CSPLineItem li WITH (NOLOCK) 
    INNER JOIN SupportOptionType sot WITH (NOLOCK) 
     ON sot.SupportOptionType_ID = li.SupportOptionType_ID 
    GROUP BY sot.SupportOptionType_ID 
    ) Classic 
LEFT JOIN (
    SELECT sot.SupportOptionType_ID, 
     COUNT(so.SupportOption_ID) NumberOfLines 
    FROM SupportOption so WITH (NOLOCK) 
    INNER JOIN SupportOptionType sot WITH (NOLOCK) 
     ON sot.SupportOptionType_ID = so.SupportOptionType_ID 
    GROUP BY sot.SupportOptionType_ID 
    ) MEN 

但我發現了以下錯誤接近最後一行:

在一個條件有望

任何人指定的上下文非布爾類型的表達式,請幫忙。

+0

你不是兩個表連接在一起。 IE: - >)MEN ON MEN.SupportOptionType_ID = Classic.SupportOptionType_ID – Kahn 2014-08-28 10:29:01

+2

你正在子查詢上執行'LEFT JOIN's,但沒有爲它們指定'ON'子句。 – 2014-08-28 10:29:03

回答

1

您需要的ON子句中添加:

SELECT DISTINCT sot.SupportOptionType_ID, (Classic.NumberOfLines + MEN.NumberOfLines) TotalNumberOfLines 
FROM SupportOptionType sot WITH (NOLOCK) 
LEFT JOIN 
(SELECT sot.SupportOptionType_ID,COUNT(li.CSPLineItem_ID) NumberOfLines 
FROM CSPLineItem li WITH (NOLOCK) 
INNER JOIN SupportOptionType sot WITH (NOLOCK) ON sot.SupportOptionType_ID = li.SupportOptionType_ID 
GROUP BY sot.SupportOptionType_ID 
) Classic 
ON sot.SupportOptionType_ID=Classic.SupportOptionType_ID 
LEFT JOIN 
(SELECT sot.SupportOptionType_ID,COUNT(so.SupportOption_ID) NumberOfLines 
FROM SupportOption so WITH (NOLOCK) 
INNER JOIN SupportOptionType sot WITH (NOLOCK) ON sot.SupportOptionType_ID = so.SupportOptionType_ID 
GROUP BY sot.SupportOptionType_ID 
)MEN 
ON sot.SupportOptionType_ID=MEN.SupportOptionType_ID 
0
SELECT distinct sot.SupportOptionType_ID, (Classic.NumberOfLines + MEN.NumberOfLines) TotalNumberOfLines 
FROM SupportOptionType sot WITH (NOLOCK) 
LEFT JOIN 
(SELECT sot.SupportOptionType_ID,COUNT(li.CSPLineItem_ID) NumberOfLines 
FROM CSPLineItem li WITH (NOLOCK) 
INNER JOIN SupportOptionType sot WITH (NOLOCK) ON sot.SupportOptionType_ID = li.SupportOptionType_ID 
GROUP BY sot.SupportOptionType_ID 
) Classic 
on sot.SupportOptionType_ID=Classic.SupportOptionType_ID 
LEFT JOIN 
(SELECT sot.SupportOptionType_ID,COUNT(so.SupportOption_ID) NumberOfLines 
FROM SupportOption so WITH (NOLOCK) 
INNER JOIN SupportOptionType sot WITH (NOLOCK) ON sot.SupportOptionType_ID = so.SupportOptionType_ID 
GROUP BY sot.SupportOptionType_ID 
)MEN 
on sot.SupportOptionType_ID=MEN.SupportOptionType_ID 
相關問題