Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Maybe I need a subquery for this.

Author  Topic 

ultradiv
Starting Member

41 Posts

Posted - 2012-10-26 : 04:32:48
Hi team.
my question involves using the latest result from multiple results in a joined table query

The code below will set up the #tables and display 3 results
the 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 data

IF OBJECT_ID('TempDB..#SR','U') IS NOT NULL
DROP TABLE #SR
CREATE 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 ON

INSERT INTO #SR
([SRID],[ServiceID],[DateAdded],[Status])
SELECT '23341','19','Sep 23 2011 11:21PM','4' UNION ALL
SELECT '23342','19','Sep 23 2012 11:32PM','3' UNION ALL
SELECT '23343','21','Sep 24 2012 7:07AM','2' UNION ALL
SELECT '23344','14','Oct 21 2012 10:35PM','4' UNION ALL
SELECT '23345','27','Oct 22 2012 9:47AM','1' UNION ALL
SELECT '23346','35','Oct 22 2012 10:37AM','4' UNION ALL
SELECT '23347','35','Oct 22 2012 2:27PM','1' UNION ALL
SELECT '23348','12','Oct 22 2012 2:29PM','0' UNION ALL
SELECT '23349','19','Oct 22 2012 2:33PM','0' UNION ALL
SELECT '23350','15','Oct 22 2012 5:25PM','0' UNION ALL
SELECT '23351','15','Oct 22 2000 5:33PM','0'

SET IDENTITY_INSERT #SR OFF



IF OBJECT_ID('TempDB..#DS','U') IS NOT NULL
DROP TABLE #DS
CREATE 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 ALL
SELECT '13','8','14','168' UNION ALL
SELECT '14','8','15','1' UNION ALL
SELECT '15','8','9','1' UNION ALL
SELECT '16','4','18','336' UNION ALL
SELECT '20','5','4','0' UNION ALL
SELECT '23','5','21','8' UNION ALL
SELECT '28','4','12','72' UNION ALL
SELECT '37','4','19','13'

SET IDENTITY_INSERT #DS OFF

IF OBJECT_ID('TempDB..#SR_Dept','U') IS NOT NULL
DROP TABLE #SR_Dept

CREATE 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 ALL
SELECT '2','23341','4','1' UNION ALL
SELECT '3','23342','1','1' UNION ALL
SELECT '4','23343','1','1' UNION ALL
SELECT '5','23344','8','1' UNION ALL
SELECT '6','23345','5','1' UNION ALL
SELECT '7','23346','1','1' UNION ALL
SELECT '8','23347','1','1' UNION ALL
SELECT '9','23348','4','1' UNION ALL
SELECT '10','23349','5','1' UNION ALL
SELECT '11','23349','4','1' UNION ALL
SELECT '12','23350','1','1' UNION ALL
SELECT '13','23351','8','1'

SET IDENTITY_INSERT #SR_Dept OFF


IF OBJECT_ID('TempDB..#SRAction','U') IS NOT NULL
DROP TABLE #SRAction

CREATE 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 ALL
SELECT '40','23343','17','Sep 24 2012 7:07AM' UNION ALL
SELECT '41','23342','19','Sep 24 2012 9:47PM' UNION ALL
SELECT '42','23342','20','Sep 24 2012 9:47PM' UNION ALL
SELECT '43','23341','21','Oct 1 2012 7:52PM' UNION ALL
SELECT '44','23341','3','Oct 1 2012 7:58PM' UNION ALL
SELECT '45','23341','23','Oct 1 2012 7:58PM' UNION ALL
SELECT '46','23341','21','Oct 1 2012 7:58PM' UNION ALL
SELECT '47','23343','18','Oct 8 2012 10:28PM' UNION ALL
SELECT '48','23344','17','Oct 8 2012 10:35PM' UNION ALL
SELECT '49','23345','17','Oct 22 2012 10:41AM' UNION ALL
SELECT '50','23342','19','Oct 22 2012 10:42AM' UNION ALL
SELECT '51','23346','17','Oct 22 2012 11:02AM' UNION ALL
SELECT '52','23346','18','Oct 22 2012 11:03AM' UNION ALL
SELECT '53','23346','21','Oct 22 2012 11:13AM' UNION ALL
SELECT '54','23347','17','Oct 22 2012 2:28PM' UNION ALL
SELECT '55','23344','18','Oct 22 2012 3:33PM' UNION ALL
SELECT '56','23344','21','Oct 22 2012 3:33PM'

SET IDENTITY_INSERT #SRAction OFF



/*the above sets up the tables

I need several different queries from them

These are the queries I have tried already

First, 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 #SRAction
DROP TABLE #SR
DROP TABLE #DS
DROP TABLE #SR_Dept


--Any help will be much appreciated
   

- Advertisement -