2016-06-22 88 views
0

我甚至不知道如何正確地說出這個問題。我正在嘗試編寫一個查詢案例(狀態:關閉)的查詢,檢查特定用戶是否輸入了一個便箋,如果是,請檢查該便箋後面是否添加了便條(該便條會說明案件重新開放或不)。下面是我有:我可以使用子查詢連接字符串嗎?

SELECT DISTINCT scr.CaseId, scr.AssignedSRIdent, s.RepFullName FROM dbo.SupportCaseRoot scr 
INNER JOIN #comparison1 c ON c.CaseId = scr.CaseId 
INNER JOIN #comparison2 c2 ON c2.CaseId = scr.CaseId 
INNER JOIN dbo.servicereproot s ON s.SRIdent = scr.SRIdent 
INNER JOIN dbo.SupportCaseNote scn ON scn.CaseId = scr.CaseId 
WHERE scr.StatusId IN (4, 36, 37, 39, 28) 
AND c.DateCreated > c2.DateCreated 
AND scn.Description LIKE '%status%of%' + ' ' + (SELECT scs.Description FROM dbo.SupportCaseStatus scs WHERE scs.StatusId NOT IN (4, 36, 37, 39, 28)) 
ORDER BY scr.CaseId 

當我運行它,我得到這個錯誤:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 

我假設的最後一個AND語句和我在級聯愚蠢的嘗試,因爲。我希望它找到「[找到所有描述]的狀態」。任何人有任何建議?

回答

0

你可以使用一些解決方案:

  • 使用「頂部1」或「行偏移」久違的子查詢或單個值
  • 使用聚合函數返回單一(爲前像Max。)在子查詢或值
  • 與一個函數返回
0

這裏的誤差是在子表返回更個單值替換該子查詢一個值。

隔離子查詢並查看您獲得的結果。子查詢最多需要返回一個結果才能「可連接」。

0

嘗試在子查詢中相匹配,就像這樣:

SELECT DISTINCT scr.CaseId, scr.AssignedSRIdent, s.RepFullName 
FROM dbo.SupportCaseRoot scr 
INNER JOIN #comparison1 c ON c.CaseId = scr.CaseId 
INNER JOIN #comparison2 c2 ON c2.CaseId = scr.CaseId 
INNER JOIN dbo.servicereproot s ON s.SRIdent = scr.SRIdent 
INNER JOIN dbo.SupportCaseNote scn ON scn.CaseId = scr.CaseId 
WHERE scr.StatusId IN (4, 36, 37, 39, 28) 
AND c.DateCreated > c2.DateCreated 
AND EXISTS(
    SELECT 1 FROM dbo.SupportCaseStatus scs WHERE scs.StatusId NOT IN (4, 36, 37, 39, 28) 
    AND scn.Description LIKE ('%status%of%' + ' ' + scs.Description) 
) 
ORDER BY scr.CaseId 
相關問題