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


Where query time between 6am and 6pm


Where query time between 6am and 6pm

Author
Message
spyfly
spyfly
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 48
I am trying to modify my query so that it will only look at the data between 6am and 6pm instead of the whole 24 hours, I have tried a few things but have had no luck

Here is my current query

SET NOCOUNT OFF
SET ROWCOUNT 0
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DATEADD(d, DATEDIFF(d, 0, DATEADD(m, -1, DATEADD(d, 1 - day(getdate()), getdate()))), 0)
SET @EndDate = DATEADD(ms, -2,DATEADD(d, DATEDIFF(d, 0, DATEADD(d, 1 - day(getdate()), getdate())), 0))
SELECT Interfaces.InterfaceId,
Nodes.NodeID,
Nodes.Caption AS NodeName,
Nodes.VendorIcon AS Vendor_Icon,
Interfaces.Caption AS Interface_Caption,
Interfaces.InterfaceIcon AS Interface_Icon,
Maxbps_In95,
Maxbps_Out95,
Maxbps_95,
Interfaces.InterfaceSpeed AS Interface_Speed,
Recieve_Percentage = (Maxbps_In95 / Interfaces.InterfaceSpeed ) * 100,
Transmit_Percentage = (Maxbps_Out95 / Interfaces.InterfaceSpeed) * 100
FROM Nodes
INNER JOIN Interfaces ON Nodes.NodeID = Interfaces.NodeID
INNER JOIN (
SELECT InterfaceID,
dbo.GetInBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_In95,
dbo.GetOutBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_Out95,
dbo.GetMaxBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_95
FROM InterfaceTraffic
WHERE InterfaceTraffic.DateTime >= @StartDate AND InterfaceTraffic.DateTime <= @EndDate
GROUP BY InterfaceID
) TrafficStat
ON Interfaces.InterfaceID = TrafficStat.InterfaceID
WHERE (1=1) AND
( (Nodes.TBU = 'TBU') AND (Interfaces.Link_Type LIKE '%Primary%') )


Thomas Rushton
Thomas Rushton
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: Moderators
Points: 11798 Visits: 6195
What I would do is firstly change your Start/EndDate calculations to form a bit simpler to understand:

SET @EndDate = DATEADD(MONTH,DATEDIFF(MONTH,'20100101',GETDATE()),'20100101'); -- first of this month
SET @StartDate = DATEADD(MONTH,-1,@EndDate); -- first of last month


and then change the WHERE clause:

WHERE InterfaceTraffic.DateTime >= @StartDate AND InterfaceTraffic.DateTime < @EndDate
AND DATEPART(HOUR,InterfaceTraffic.DateTime) >= 6 AND DATEPART(HOUR, InterfaceTraffic.DateTime) < 18




anthony.green
anthony.green
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62610 Visits: 8594
You will need to convert the time aspect into a time only column then filter out where >= 6 and < 18, something like the below.
SET NOCOUNT OFF
SET ROWCOUNT 0
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DATEADD(d, DATEDIFF(d, 0, DATEADD(m, -1, DATEADD(d, 1 - day(getdate()), getdate()))), 0)
SET @EndDate = DATEADD(ms, -2,DATEADD(d, DATEDIFF(d, 0, DATEADD(d, 1 - day(getdate()), getdate())), 0))
SELECT Interfaces.InterfaceId,
Nodes.NodeID,
Nodes.Caption AS NodeName,
Nodes.VendorIcon AS Vendor_Icon,
Interfaces.Caption AS Interface_Caption,
Interfaces.InterfaceIcon AS Interface_Icon,
Maxbps_In95,
Maxbps_Out95,
Maxbps_95,
Interfaces.InterfaceSpeed AS Interface_Speed,
Recieve_Percentage = (Maxbps_In95 / Interfaces.InterfaceSpeed ) * 100,
Transmit_Percentage = (Maxbps_Out95 / Interfaces.InterfaceSpeed) * 100
FROM Nodes
INNER JOIN Interfaces ON Nodes.NodeID = Interfaces.NodeID
INNER JOIN (
SELECT InterfaceID,
dbo.GetInBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_In95,
dbo.GetOutBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_Out95,
dbo.GetMaxBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_95
FROM InterfaceTraffic
WHERE InterfaceTraffic.DateTime >= @StartDate AND InterfaceTraffic.DateTime <= @EndDate AND CONVERT (time, InterfaceTraffic.DateTime) >= '06:00:00' AND CONVERT(time, InterfaceTraffic.DateTime) < '18:00:00'
GROUP BY InterfaceID
) TrafficStat
ON Interfaces.InterfaceID = TrafficStat.InterfaceID
WHERE (1=1) AND
( (Nodes.TBU = 'TBU') AND (Interfaces.Link_Type LIKE '%Primary%') )




How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


spyfly
spyfly
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 48
Perfect, I was missing the DATEPART, I was using DateTime that was my issue

thanks
ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96494 Visits: 20675

-- Here's how to get the time portion of your datetime.

-- Note that the TIME function has a parameter, check it

-- is correct for you using BOL

SELECT CAST(GETDATE() AS TIME(0))

-- Here's how to use it

SELECT [TimePart]

FROM (VALUES (

CAST('05:00:00' AS TIME(0))),

('05:59:59'),

('06:00:00'),

('17:59:59'),

('18:00:00'),

('18:01:01')) d ([TimePart])

WHERE [TimePart] >= '06:00:00' AND [TimePart] <= '18:00:00'



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
spyfly
spyfly
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 48
I have tried the solutions above and no matter what time frame I put in my data does not change.

Here is my current query

SET NOCOUNT OFF
SET ROWCOUNT 0
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = DATEADD(d, DATEDIFF(d, 0, DATEADD(m, -1, DATEADD(d, 1 - day(getdate()), getdate()))), 0)
SET @EndDate = DATEADD(ms, -2,DATEADD(d, DATEDIFF(d, 0, DATEADD(d, 1 - day(getdate()), getdate())), 0))
SELECT
Interfaces.InterfaceId,
Nodes.NodeID,
Nodes.Caption AS NodeName,
Nodes.VendorIcon AS Vendor_Icon,
Interfaces.Caption AS Interface_Caption,
Interfaces.InterfaceIcon AS Interface_Icon,
Maxbps_In95,
Maxbps_Out95,
Maxbps_95,
Interfaces.InterfaceSpeed AS Interface_Speed,
round((Maxbps_In95/Maxbps_95*100),0) as In_Percent,
round((Maxbps_Out95/Maxbps_95*100),0) as Out_Percent

FROM Nodes
INNER JOIN Interfaces ON Nodes.NodeID = Interfaces.NodeID
INNER JOIN (
SELECT InterfaceID,
dbo.GetInBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_In95,
dbo.GetOutBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_Out95,
dbo.GetMaxBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_95
FROM InterfaceTraffic
WHERE InterfaceTraffic.DateTime >= @StartDate AND InterfaceTraffic.DateTime <= @EndDate AND DATEPART(HOUR,InterfaceTraffic.DateTime) >= 8 AND DATEPART(HOUR, InterfaceTraffic.DateTime) < 14
GROUP BY InterfaceID
) TrafficStat
ON Interfaces.InterfaceID = TrafficStat.InterfaceID
WHERE (1=1) AND
( (Nodes.TBU = 'MVA') AND (Interfaces.Link_Type LIKE '%Primary%') )

ChrisM@Work
ChrisM@Work
SSC Guru
SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)SSC Guru (96K reputation)

Group: General Forum Members
Points: 96494 Visits: 20675
Have you checked that the inner query is working correctly?

SELECT InterfaceID, [DateTime],

dbo.GetInBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_In95,

dbo.GetOutBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_Out95,

dbo.GetMaxBps95th(InterfaceID, @StartDate, @EndDate) AS Maxbps_95

FROM InterfaceTraffic

WHERE [DateTime] >= @StartDate

AND [DateTime] <= @EndDate

AND DATEPART(HOUR,[DateTime]) >= 8

AND DATEPART(HOUR, [DateTime]) < 14



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Thomas Rushton
Thomas Rushton
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: Moderators
Points: 11798 Visits: 6195
I thought you were looking for records where the hour was between 06:00 & 18:00, but your query says between 08:00 & 14:00...
spyfly
spyfly
SSC Journeyman
SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)SSC Journeyman (79 reputation)

Group: General Forum Members
Points: 79 Visits: 48
ThomasRushton - Thursday, October 26, 2017 8:02 AM
I thought you were looking for records where the hour was between 06:00 & 18:00, but your query says between 08:00 & 14:00...

Thomas, I changed it around just testing... Honestly I was just using 06:00 and 18:00 as a baseline, but even if I sent the time to 11:00 & 12:00 it still shows the same percentage of usage at it does for a 24 hour day, so I know that data is incorrect it should be much less for a 1 hour window.

Thomas Rushton
Thomas Rushton
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: Moderators
Points: 11798 Visits: 6195
spyfly - Thursday, October 26, 2017 9:26 AM
ThomasRushton - Thursday, October 26, 2017 8:02 AM
I thought you were looking for records where the hour was between 06:00 & 18:00, but your query says between 08:00 & 14:00...

Thomas, I changed it around just testing... Honestly I was just using 06:00 and 18:00 as a baseline, but even if I sent the time to 11:00 & 12:00 it still shows the same percentage of usage at it does for a 24 hour day, so I know that data is incorrect it should be much less for a 1 hour window.

Hang on - are these the percentages that are calculated in the functions GetInBps95 etc? Those functions appear to take a date range only - perhaps this is where the problem is?

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