Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

general SQL query needed from many to many table joins Expand / Collapse
Author
Message
Posted Thursday, October 25, 2012 2:41 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, March 27, 2014 12:56 AM
Points: 34, Visits: 60
--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','18','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, 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 rows in 'SR that has a [Status] of less than 2 and where the #DS.ServiceLevel (number of hours) is added to the #SR.DateAdded and 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, count rows in #SR has a [Status] of 4 and where the service level datetime (with hours[#DS.ServiceLevel] added) is <= #SRAction.EntryDateTime
(this one I cant get to work because there are/maybe more than one #SRAction for each #SR that is #SRAction.ActionID=21 and so can only determine the datetime(SRAction.EntryDateTime) incorrectly)
*/

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

--Any help will be much appreciated

Post #1377298
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse