Hi team.my question involves using the latest result from multiple results in a joined table queryThe code below will set up the #tables and display 3 resultsthe first (see the comments) is simple enough, although working, I am wondering if it is the most efficient method.The second query like the first but with the datetime from the #SR table compared with now (getUTCdate()) works ok but again I feel that improvements can be had.The third query is my problem as it needs to compare the datetime from the #SR table against a field in another table (#SRAction.EntryDateTime) BUT there can be many rows in #SRAction when joined by SRID.I cant get my limited grey matter around the issue, can you help me please?--To set up dataIF OBJECT_ID('TempDB..#SR','U') IS NOT NULL DROP TABLE #SRCREATE TABLE #SR( [SRID] [int] IDENTITY(1,1) PRIMARY KEY CLUSTERED, [ServiceID] [int], [DateAdded] [datetime],--entered as GETUTCDATE() [Status] [int] --0,1,2,3 or 4 ) SET IDENTITY_INSERT #SR ONINSERT INTO #SR ([SRID],[ServiceID],[DateAdded],[Status])SELECT '23341','19','Sep 23 2011 11:21PM','4' UNION ALLSELECT '23342','19','Sep 23 2012 11:32PM','3' UNION ALLSELECT '23343','21','Sep 24 2012 7:07AM','2' UNION ALLSELECT '23344','14','Oct 21 2012 10:35PM','4' UNION ALLSELECT '23345','27','Oct 22 2012 9:47AM','1' UNION ALLSELECT '23346','35','Oct 22 2012 10:37AM','4' UNION ALLSELECT '23347','35','Oct 22 2012 2:27PM','1' UNION ALLSELECT '23348','12','Oct 22 2012 2:29PM','0' UNION ALLSELECT '23349','19','Oct 22 2012 2:33PM','0' UNION ALLSELECT '23350','15','Oct 22 2012 5:25PM','0' UNION ALLSELECT '23351','15','Oct 22 2000 5:33PM','0' SET IDENTITY_INSERT #SR OFFIF OBJECT_ID('TempDB..#DS','U') IS NOT NULL DROP TABLE #DSCREATE TABLE #DS( [DSID] [int] IDENTITY(1,1) PRIMARY KEY CLUSTERED, [DepartmentID] [int], [ServiceID] [int], [ServiceLevel] [int] )SET IDENTITY_INSERT #DS ON INSERT INTO #DS ([DSID],[DepartmentID],[ServiceID],[ServiceLevel])SELECT '6','4','10','168' UNION ALLSELECT '13','8','14','168' UNION ALLSELECT '14','8','15','1' UNION ALLSELECT '15','8','9','1' UNION ALLSELECT '16','4','18','336' UNION ALLSELECT '20','5','4','0' UNION ALLSELECT '23','5','21','8' UNION ALLSELECT '28','4','12','72' UNION ALLSELECT '37','4','19','13'SET IDENTITY_INSERT #DS OFFIF OBJECT_ID('TempDB..#SR_Dept','U') IS NOT NULL DROP TABLE #SR_DeptCREATE TABLE #SR_Dept( -- a join table joining SR & Department on SRID=DepartmentID [SRDeptID] [int] IDENTITY(1,1) PRIMARY KEY CLUSTERED , [SRID] [int], [DepartmentID] [int], [SPID] [int])SET IDENTITY_INSERT #SR_Dept ON INSERT INTO #SR_Dept ([SRDeptID],[SRID],[DepartmentID],[SPID])SELECT '1','23341','5','1' UNION ALLSELECT '2','23341','4','1' UNION ALLSELECT '3','23342','1','1' UNION ALLSELECT '4','23343','1','1' UNION ALLSELECT '5','23344','8','1' UNION ALLSELECT '6','23345','5','1' UNION ALLSELECT '7','23346','1','1' UNION ALLSELECT '8','23347','1','1' UNION ALLSELECT '9','23348','4','1' UNION ALLSELECT '10','23349','5','1' UNION ALLSELECT '11','23349','4','1' UNION ALLSELECT '12','23350','1','1' UNION ALLSELECT '13','23351','8','1'SET IDENTITY_INSERT #SR_Dept OFFIF OBJECT_ID('TempDB..#SRAction','U') IS NOT NULL DROP TABLE #SRActionCREATE TABLE #SRAction( [SRActionID] [int] IDENTITY(1,1) PRIMARY KEY CLUSTERED , [SRID] [int], [ActionID] [int], [EntryDateTime] [datetime])SET IDENTITY_INSERT #SRAction ON INSERT INTO #SRAction ([SRActionID],[SRID],[ActionID],[EntryDateTime])SELECT '39','23342','18','Sep 23 2012 11:38PM' UNION ALLSELECT '40','23343','17','Sep 24 2012 7:07AM' UNION ALLSELECT '41','23342','19','Sep 24 2012 9:47PM' UNION ALLSELECT '42','23342','20','Sep 24 2012 9:47PM' UNION ALLSELECT '43','23341','21','Oct 1 2012 7:52PM' UNION ALLSELECT '44','23341','3','Oct 1 2012 7:58PM' UNION ALLSELECT '45','23341','23','Oct 1 2012 7:58PM' UNION ALLSELECT '46','23341','21','Oct 1 2012 7:58PM' UNION ALLSELECT '47','23343','18','Oct 8 2012 10:28PM' UNION ALLSELECT '48','23344','17','Oct 8 2012 10:35PM' UNION ALLSELECT '49','23345','17','Oct 22 2012 10:41AM' UNION ALLSELECT '50','23342','19','Oct 22 2012 10:42AM' UNION ALLSELECT '51','23346','17','Oct 22 2012 11:02AM' UNION ALLSELECT '52','23346','18','Oct 22 2012 11:03AM' UNION ALLSELECT '53','23346','21','Oct 22 2012 11:13AM' UNION ALLSELECT '54','23347','17','Oct 22 2012 2:28PM' UNION ALLSELECT '55','23344','18','Oct 22 2012 3:33PM' UNION ALLSELECT '56','23344','21','Oct 22 2012 3:33PM' SET IDENTITY_INSERT #SRAction OFF/*the above sets up the tablesI need several different queries from themThese are the queries I have tried alreadyFirst, get count of all Rows in #SR where #SR_Dept.SPID=1 - Working but I'm sure can be improved*/SELECT count(distinct #SR.SRID)FROM #SR INNER JOIN #DS ON #SR.ServiceID = #DS.ServiceID INNER JOIN #SR_Dept ON #SR.SRID = #SR_Dept.SRID where #SR_Dept.SPID=1/*Second, count of all rows in #SR that have a [Status] of less than 2 and where the #DS.ServiceLevel (number of hours) when added to the #SR.DateAdded date is before now(utc)*/SELECT count(distinct #SR.SRID)FROM #SR INNER JOIN #DS ON #SR.ServiceID = #DS.ServiceID INNER JOIN #SR_Dept ON #SR.SRID = #SR_Dept.SRID where dateadd(HH,case when #DS.ServiceLevel IS null THEN 100000 ELSE #DS.ServiceLevel END,#SR.DateAdded) < GETDATE() AND #SR.[Status]<2 AND #SR_Dept.SPID=1 /*Third, get count of all rows in #SR that have a [Status] of 4 and where the service level datetime (with hours[#DS.ServiceLevel] added) is <= #SRAction.EntryDateTime (this one I can't get to work because there are/maybe more than one #SRAction for each #SR that equals #SRAction.ActionID=21 and so the query cannot determine the datetime(SRAction.EntryDateTime) correctly)*/SELECT count(distinct #SR.SRID)FROM #SR INNER JOIN #DS ON #SR.ServiceID = #DS.ServiceID INNER JOIN #SR_Dept ON #SR.SRID = #SR_Dept.SRID INNER JOIN #SRAction ON #SR.SRID = #SRAction.SRID where dateadd(HH,case when #DS.ServiceLevel IS null THEN 100000 ELSE #DS.ServiceLevel END,#SR.DateAdded) <= #SRAction.EntryDateTime AND #SR.[Status] = 4 AND #SR_Dept.SPID=1 AND #SRAction.ActionID=21 --clean up DROP TABLE #SRActionDROP TABLE #SRDROP TABLE #DSDROP TABLE #SR_Dept --Any help will be much appreciated