我相信 Group By 可以解决以下问题,但也许是 Select Distinct。不管怎样,我恭敬地转向你:
目标:
通过协议合并,然后是 LaborCode,然后是 Schedule。我为一家提供多种服务的公司工作,我想选择任何日期范围,并指定我们每周执行每项服务的日期。在一个月的时间里,我们将有 (4) 个星期一 - 我只想看到一个星期一。附带说明一下,我尝试过 XML Path,但它似乎不起作用。
编码:
Select Top 100 Percent Agreements.AgrmntID, JobSites.SiteName,
Customers.CompanyName, LaborCodeTypes.Description As LaborCode, DateName(dw,
AgreementSchedules.SchedDate) as Schedule
From Agreements Inner Join
AgreementSchedules On Agreements.AgrmntID = AgreementSchedules.AgrmntID
Inner Join
Customers On Agreements.CustID = Customers.CustID Inner Join
JobSites On Agreements.CustSiteID = JobSites.CustSiteID Left Outer Join
LaborCodeTypes On AgreementSchedules.RepairID = LaborCodeTypes.RepairID
Where Agreements.Status = 2
数据(时间表源自 AgreementSchedules.SchedDate) - 选择了 2 周:
AgrmntID SiteName Company LaborCode Schedule
---------- ---------- ---------- ---------- ----------
1 JOB01 COMP01 Sweeping Monday
1 JOB01 COMP01 Sweeping Wednesday
1 JOB01 COMP01 Sweeping Monday
1 JOB01 COMP01 Sweeping Wednesday
1 JOB01 COMP01 Porter Thursday
1 JOB01 COMP01 Porter Thursday
2 JOB02 COMP02 Landscaping Tuesday
2 JOB02 COMP02 Landscaping Tuesday
所需的输出:
AgrmntID SiteName Company LaborCode Schedule
---------- ---------- ---------- ---------- ----------
1 JOB01 COMP01 Sweeping Monday
1 JOB01 COMP01 Sweeping Wednesday
1 JOB01 COMP01 Porter Thursday
2 JOB02 COMP02 Landscaping Tuesday
非常感谢你。几天来我一直试图解决这个问题,但没有运气。