I believe a Group By would solve the following, but maybe it’s Select Distinct. Either way, I respectfully turn to you:
The Goal:
To consolidate by Agreement then LaborCode then Schedule. I work for a company that performs multiple services and I want to take any range of dates and specify what DAYS OF THE WEEK we perform each service. Over the period of a month we would have (4) Mondays – I want to just see ONE Monday. As a side note, I have tried XML Path but it doesn’t seem to work.
The Code:
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
The Data (Schedule is derived from AgreementSchedules.SchedDate) – 2 weeks selected:
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
The Desired Output:
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
Thank you SO much in advance. I’ve been trying to figure this out for days with no luck.
Either would work.
I’d go for DISTINCT