WITH Drivers AS ( SELECT J.DriverID , MAX(J.CollectionDatetime) AS LastAllocStDate FROM JOBProgress J GROUP BY J.DriverID ) , A AS ( SELECT j.ID , j.ClientName , j.DriverID , CAST(j.CollectionDateTime AS DATE) AS AllocStDate , j.Allocation , CAST(j.AllocEndDate AS DATE) AS AllocEndDate FROM JOBProgress j INNER JOIN Drivers d ON j.driverId = d.driverId AND j.CollectionDatetime = d.LastAllocStDate ) , T AS ( SELECT B.NAME , A.Allocation , A.AllocStDate , A.AllocEndDate , A.ClientName , A.ID , IIF(A.AllocStDate <= CAST(GETDATE() AS DATE) AND A.AllocEndDate >= CAST(GETDATE() AS DATE), 'Allocated', NULL) AS OnJob FROM A INNER JOIN FieldResource B ON A.DriverID = B.ID ) , Z AS ( SELECT DISTINCT T.Name , T.Allocation , T.AllocStDate , T.AllocEndDate , IIF(T.OnJob IS NULL, NULL, T.ClientName) AS ClientName , IIF(T.OnJob IS NULL, NULL, T.ID) AS ID , IIF(T.OnJob IS NULL, 'YES', NULL) AS NotAllocated , T.OnJob AS Allocated FROM T ) SELECT Z.AllocStDate , Z.AllocEndDate , Z.ClientName , Z.ID , Z.NotAllocated , Z.Allocated , IIF( 1=1 AND EXISTS ( SELECT * FROM Transfer TT WHERE TT.Driver = Z.Name AND TT.Comments = 'Allocated' ), 'AllocatedByComments', '') AS IsAllocatedByComments FROM Z ORDER BY Z.Allocated DESC, IsAllocatedByComments desc;
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE