2015-08-21 106 views
-2

我正在編寫一個查詢來訪問同一個數據庫中的多個表。對於在SELECT語句中的一列,我需要返回SQL select語句檢查兩列並返回第三個值

Table1.Column4 where Table1.Column = Table3.Column1 AND Table1.Column2 = Table4.Column1

我把它寫成:

SELECT AccNum.FieldValue 
FROM PersonFieldValuesVW 
INNER JOIN PersonFieldValuesVW AccNum 
ON AccNum.PersonId = InPerson.PersonId 
    INNER JOIN InPerson 
    ON InPerson.IncidentId = Incident.Id 
WHERE AccNum.FieldDescr like '%Account Number%') as [Account Number], 

這是返回錯誤:

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

這裏是完整的查詢,任何援助將不勝感激。

SELECT DISTINCT 
    CaseNum as [Case Number], 
    ALCategoryVW.Category as [Category], 
    ALCategoryVW.SubCategory as [Sub Category], 
    InAssign.AssignToName as [Assigned To], 
    ReportedDate as [Open Date], 
    EndDate as [Closed Date], --This a placeholder for a closed date 
    [Status], 
    SiteLoc1.Descr as [Loss Location], 
    LocDetails as [Loss Cost Center], 
    SiteLoc1.Region as [Region], 
    SiteLoc1.SubRegion as [Sub Region], 
    -- SiteLoc2.Descr as [Location Description], **Need this though returning all for the location? 
    CASE WHEN SAR.FieldId = '604NU' and SAR.FieldValue <> 'False' THEN 'YES' ELSE 'NO' END as [SAR Required], 
    Summary as [Incident Summary], 
    Disposition as [Case Disposition], 
    (
     SELECT AccNum.FieldValue 
     FROM PersonFieldValuesVW 
     INNER JOIN PersonFieldValuesVW AccNum ON AccNum.PersonId = InPerson.PersonId 
     INNER JOIN InPerson ON InPerson.IncidentId = Incident.Id 
     WHERE AccNum.FieldDescr like '%Account Number%' 
    ) as [Account Number], 
    FORMAT(AuditItemDetail.ItemValue, '#,###') as [Potential Loss], 
    FORMAT(AuditItemDetail.ItemValue - AuditItemDetail.PreventedExposureAmount, '#,###') as [Actual Loss] 
FROM Incident 
INNER JOIN ALCategoryVW ON ALCategoryVW.IncidentId = Incident.Id 
INNER JOIN InAssign ON InAssign.IncidentId = Incident.Id 
INNER JOIN SiteLoc1 ON SiteLoc1.Id = Incident.LocId 
INNER JOIN SiteLoc2 ON SiteLoc2.SiteLoc1Id = SiteLoc1.Id 
INNER JOIN IncidentFieldValuesVW SAR ON SAR.IncidentId = Incident.Id 
INNER JOIN InItem ON InItem.IncidentId = Incident.Id 
INNER JOIN AuditItemDetail ON AuditItemDetail.ItemId = InItem.ItemId 
INNER JOIN InPerson ON InPerson.IncidentId = Incident.Id 
INNER JOIN PersonFieldValuesVW AccNum ON AccNum.PersonId = InPerson.PersonId 
+1

你看過錯誤信息嗎?你的子查詢在允許返回唯一一個的上下文中返回多個值。例如2+記錄和/或2+字段。 –

+0

爲什麼你首先需要一個子查詢?您的主要查詢中已經有該表格。 –

+0

@MarcB - 我看到,但我不知道如何返回所有行。感謝您的快速回復。 –

回答

1

這應該可以解決您的錯誤,但我不確定它在邏輯上是否正確。如果在子選擇中有兩個記錄被返回,哪一個是「正確的」。

SELECT DISTINCT 
    CaseNum as [Case Number], 
    ALCategoryVW.Category as [Category], 
    ALCategoryVW.SubCategory as [Sub Category], 
    InAssign.AssignToName as [Assigned To], 
    ReportedDate as [Open Date], 
    EndDate as [Closed Date], --This a placeholder for a closed date 
    [Status], 
    SiteLoc1.Descr as [Loss Location], 
    LocDetails as [Loss Cost Center], 
    SiteLoc1.Region as [Region], 
    SiteLoc1.SubRegion as [Sub Region], 
    -- SiteLoc2.Descr as [Location Description], **Need this though returning all for the location? 
    CASE WHEN SAR.FieldId = '604NU' and SAR.FieldValue <> 'False' THEN 'YES' ELSE 'NO' END as [SAR Required], 
    Summary as [Incident Summary], 
    Disposition as [Case Disposition], 
    AccNum.FieldValue, 
    FORMAT(AuditItemDetail.ItemValue, '#,###') as [Potential Loss], 
    FORMAT(AuditItemDetail.ItemValue - AuditItemDetail.PreventedExposureAmount, '#,###') as [Actual Loss] 
FROM Incident 
INNER JOIN ALCategoryVW ON ALCategoryVW.IncidentId = Incident.Id 
INNER JOIN InAssign ON InAssign.IncidentId = Incident.Id 
INNER JOIN SiteLoc1 ON SiteLoc1.Id = Incident.LocId 
INNER JOIN SiteLoc2 ON SiteLoc2.SiteLoc1Id = SiteLoc1.Id 
INNER JOIN IncidentFieldValuesVW SAR ON SAR.IncidentId = Incident.Id 
INNER JOIN InItem ON InItem.IncidentId = Incident.Id 
INNER JOIN AuditItemDetail ON AuditItemDetail.ItemId = InItem.ItemId 
INNER JOIN InPerson ON InPerson.IncidentId = Incident.Id 
LEFT JOIN PersonFieldValuesVW AccNum ON AccNum.PersonId = InPerson.PersonId AND AccNum.FieldDescr like '%Account Number%' 

***根據評論更新。

+0

在這種情況下,一個人可能會有很多賬號,所以我希望他們所有的人都與其他兩列相匹配。 –

+0

然後你需要加入(允許0- *匹配)你的賬號數據,而不是使用子查詢(假設一個匹配)。 – StingyJack