0

当我试图限制我的结果时,我限制得太多了。

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

我看了又看,现在来这里寻求帮助。先感谢您!!

4

2 回答 2

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'

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

于 2012-11-08T08:55:16.820 回答
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,您的回复一直为我指明正确的方向。并认为你是奇异果,即使你删除了你的回复。

于 2012-11-16T04:45:48.477 回答