子查询,嵌套选择,加入,还有别的什么? - 不确定

时间:2012-11-07 22:29:40

标签: sql sql-server-2005

当我尝试限制结果时,我对它们的限制太多了。

Select Distinct Agreements.AgrmntID, Agreements.Status, JobSites.SiteName,
  AgreementCharges.TotalSaleAmt, AgreementCharges.PartID,
  AgreementSchedules.FormanEmpID, LaborCodeTypes.RepairCode
From Agreements Inner Join
  AgreementCharges On Agreements.AgrmntID = AgreementCharges.AgrmntID Inner Join
  JobSites On JobSites.CustSiteID = Agreements.CustSiteID Left Outer Join
  AgreementSchedules On Agreements.AgrmntID = AgreementSchedules.AgrmntID
  Left Outer Join
  LaborCodeTypes On LaborCodeTypes.RepairID = AgreementSchedules.RepairID
Where Agreements.Status = 2 And AgreementCharges.PartID = 31 And
  LaborCodeTypes.RepairCode = '04 - LS'

如果没有与'04-LS'匹配的RepairCode,我仍然需要AgrmntID和TotalSaleAmt。同样,如果根本没有AgreementSchedule,我需要AgrmntID和TotalSaleAmt。

输出:

AgrmntID  Status  SiteName  TotalSaleAmt  PartID  FormanEmpID  RepairCode
-------------------------------------------------------------------------
2         2       TEST01    $100.00       31      66           04 - LS
2         2       TEST03    $300.00       31      21           04 - LS

预期产出:

AgrmntID  Status  SiteName  TotalSaleAmt  PartID  FormanEmpID  RepairCode
-------------------------------------------------------------------------
2         2       TEST01    $100.00       31      66           04 - LS
2         2       TEST02    $200.00       31
2         2       TEST03    $300.00       31      21           04 - LS

我看了看,现在正来到这里寻求帮助。提前谢谢!!

2 个答案:

答案 0 :(得分:0)

我的版本:

Select Distinct 
    Agreements.AgrmntID, 
    Agreements.Status, 
    JobSites.SiteName,
    AgreementCharges.TotalSaleAmt, 
    AgreementCharges.PartID,
    AgreementSchedules.FormanEmpID, 
    LaborCodeTypes.RepairCode
From Agreements
Inner Join JobSites On 
    JobSites.CustSiteID = Agreements.CustSiteID
Left Join AgreementCharges On 
    Agreements.AgrmntID = AgreementCharges.AgrmntID 
Left Join AgreementSchedules On 
    Agreements.AgrmntID = AgreementSchedules.AgrmntID
Left Join LaborCodeTypes On 
    LaborCodeTypes.RepairID = AgreementSchedules.RepairID
Where 
    Agreements.Status = 2 
    AND AgreementCharges.PartID = 31
    AND coalesce(LaborCodeTypes.RepairCode, '04 - LS') = '04 - LS'

P.S。如果您要向其他人展示,请下次更好地格式化您的查询:)

答案 1 :(得分:0)

这就是最终的工作:

Select Distinct Agreements.AgrmntID, Agreements.Status, JobSites.SiteName,
  AgreementCharges.TotalSaleAmt, AgreementCharges.PartID, a.FormanEmpID,
  a.RepairCode
From Agreements Inner Join
  JobSites On JobSites.CustSiteID = Agreements.CustSiteID Left Join
  AgreementCharges On Agreements.AgrmntID = AgreementCharges.AgrmntID Left Join
  (Select AgreementSchedules.AgrmntID, AgreementSchedules.FormanEmpID,
      b.RepairCode
    From AgreementSchedules Inner Join
      (Select LaborCodeTypes.RepairCode, LaborCodeTypes.RepairID
        From LaborCodeTypes) b On AgreementSchedules.RepairID = b.RepairID
    Where b.RepairCode = '04 - LS') a On Agreements.AgrmntID = a.AgrmntID
Where Agreements.Status = 2 And AgreementCharges.PartID = 31

非常感谢pkuderov,你的回答一直指向我正确的方向。并且认为你kiwirichard,即使你删除了你的回复。