SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


general SQL query needed from many to many table joins


general SQL query needed from many to many table joins

Author
Message
Andy Lucas
Andy Lucas
SSC Rookie
SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)SSC Rookie (35 reputation)

Group: General Forum Members
Points: 35 Visits: 77

--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 :-)


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search