How to create time slots of the day in SQL server - sql

I apologize in advance if you think the question has already been answered, I searched on Stack, but similar answers were much too complex for me.
I have this request in SQL :
SELECT dbo.tbTransaction.dateHeure, dbo.tbPoste.nomPoste
FROM dbo.tbTransaction
INNER JOIN dbo.tbPoste ON dbo.tbTransaction.ID_poste = dbo.tbPoste.ID_POSTE
WHERE dbo.tbPoste.id_site LIKE 17
AND dbo.tbPoste.nomPoste LIKE 'POSTE1'
AND dateHeure >= DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0)
ORDER BY dateHeure DESC
It gives me all the production of the day, the problem is : it's not precise enough because there is a team shift in the afternoon and another team shift in the night.
If you see all the production of the day, you cannot see which team produced what.
I would like to create 3 different queries that allow me to see only the production between certain hours (for example, production between 6am and noon). Do you have any advice on how to do this?

I think you will have to hardcode your time slots. For example you know that morning shift is going to work from 06:00 to 12:00.
As you know the hours, you can use HOUR function on your datetime column with appropriate >, <, = and AND operators to get the required results.
From what I have understood, it should be something like this:
HOUR(datetime column) >= 6 AND HOUR (datetime column) < 12

Okay I managed to find the query I wanted :
SELECT dbo.tbTransaction.dateHeure, dbo.tbPoste.nomPoste FROM dbo.tbTransaction INNER JOIN dbo.tbPoste ON dbo.tbTransaction.ID_poste = dbo.tbPoste.ID_POSTE WHERE dbo.tbPoste.id_site LIKE 17
AND dbo.tbPoste.nomPoste LIKE 'POSTE1'
AND dateHeure >= DATEADD(dd, DATEDIFF(dd,0,GETDATE()), 0)
AND DATEPART(HOUR, dateHeure) > 6
AND DATEPART(HOUR, dateHeure) < 13
ORDER BY dateHeure DESC

Related

How to get a SUM of a DATEDIFF but provide cut-off at 24 hours IF a single day is specified

This is actually my first question on stackoverflow, so I sincerely apologize if I am confusing or unclear.
That being said, here is my issue:
I work at a car manufacturing company and we have recently implemented the ability to track when our machines are idle. This is done by assessing the start and end time of the event called "idle_start."
Right now, I am trying to get the SUM of how long a machine is idle. Now, I figured this out BUT, some of the idle_times are LONGER than 24 hours.
So, when I specify that I only want to see the idle_time sums of ONE particular day, the sum is also counting the idle time past 24 hours.
I want to provide the option of CUTTING OFF at that 24 hours. Is this possible?
Here is the query:
{code}
SELECT r.`name` 'Producer'
, m.`name` 'Manufacturer'
-- , timediff(re.time_end, re.time_start) 'Idle Time Length'
, SEC_TO_TIME(SUM((TIME_TO_SEC(TIMEDIFF(re.time_end, re.time_start))))) 'Total Time'
, (SUM((TIME_TO_SEC(TIMEDIFF(re.time_end, re.time_start)))))/3600 'Total Time in Hours'
, (((SUM((TIME_TO_SEC(TIMEDIFF(re.time_end, re.time_start)))))/3600))/((IF(r.resource_status_id = 2, COUNT(r.resource_id), NULL))*24) 'Percent Machine is Idle divided by Machine Hours'
FROM resource_event re
JOIN resource_event_type ret
ON re.resource_event_type_id = ret.resource_event_type_id
JOIN resource_event_type reep
ON ret.parent_resource_event_type_id = reep.resource_event_type_id
JOIN resource r
ON r.`resource_id` = re.`resource_id`
JOIN manufacturer m
ON m.`manufacturer_id` = r.`manufacturer_id`
WHERE re.`resource_event_type_id` = 19
AND ret.`parent_resource_event_type_id` = 3
AND DATE_FORMAT(re.time_start, '%Y-%m-%d') >= '2013-08-12'
AND DATE_FORMAT(re.time_start, '%Y-%m-%d') <= '2013-08-18'
-- AND re.`resource_id` = 8
AND "Idle Time Length" IS NOT NULL
AND r.manufacturer_id = 13
AND r.resource_status_id = 2
GROUP BY 1, 2
Feel free to ignore the dash marks up top. And please tell me if I can be more specific as to figure this out easier and provide less headaches for those willing to help me out.
Thank you so much!
You'll want a conditional SUM, using CASE.
Not sure of syntax for your db exactly, but something like:
, SUM (CASE WHEN TIME_TO_SEC(TIMEDIFF(re.time_end, re.time_start))/3600 > 24 THEN 0
ELSE TIME_TO_SEC(TIMEDIFF(re.time_end, re.time_start))/3600
END)'Total Time in Hours'
This is not an attempt to answer your question. It's being presented as an answer rather than a comment for better formatting and readability.
You have this
AND DATE_FORMAT(re.time_start, '%Y-%m-%d') >= '2013-08-12'
AND DATE_FORMAT(re.time_start, '%Y-%m-%d') <= '2013-08-18'
in your where clause. Using functions like this make your query take longer to execute, especially on indexed fields. Something like this would run quicker.
AND re.time_start >= a date value goes here
AND re.time_start <= another date value goes here
Do you want to cut off when start/end are before/after your time range?
You can use a case to adjust it based on your timeframe, e.g. for time_start
case
when re.time_start < timestamp '2013-08-12 00:00:00'
then timestamp '2013-08-12 00:00:00'
else re.time_start
end
similar for time_end and then use those CASEs within your TIMEDIFF.
Btw, your where-condition for a given date range should be:
where time_start < timestamp '2013-08-19 00:00:00'
and time_end >= timestamp '2013-08-12 00:00:00'
This will return all idle times between 2013-08-12 and 2013-08-18

SQL DATEADD Returning Incorrect Results

I'm currently writing a query for Visual Studio 2012 and testing it in Microsoft SQL Server Management Studio using SQL Server 2008 R2.
At the moment, I've read through MSDN's article on datetimes and DATEADD, but it seems like my syntax is right. I've also read some stuff on Google as well as How to select last one week data from today's date and MySQL: DATE_ADD as well as a few more Stack Overflow articles.
The query I'm running at the moment is really simple, just:
SELECT [DateTime] AS 'Time'
,[RawStatus] AS 'Data'
FROM [ADatabase].[dbo].[SomeTable]
WHERE CustomPollerAssignmentID = '6570267A-22E1-4556-B344-EB27D9831419' --Latency Poller
AND RowID = 000042 --Some Modem Number
AND DATEADD(HOUR, -1, CURRENT_TIMESTAMP) <= DateTime
ORDER BY DateTime DESC
What I was expecting this to do was to return the data (network latency in this case) for the last hour. Instead, it's returning the last three hours and thirty minutes. When running the code with the DATEADD statement commented out, it runs just fine and returns everything for the past day or two, the maximum time this table stores latency data.
Now, the strange code above is modeled after what's below, which I know works:
SELECT NMSDS.[SnapshotTimestamp] AS 'Time'
,[LatencyValue] AS 'Latency'
FROM [ADifferentDatabase].[dbo].[AnotherTable] Late
INNER JOIN ADifferentDatabase.dbo.YetAnotherTable NMSDS ON NMS_Id = 1
AND NMSDS.SnapshotID = Late.SnapshotID
WHERE DATEADD(HOUR, -6, CURRENT_TIMESTAMP) <= NMSDS.SnapshotTimestamp
AND InrouteGroupId = #IRID
AND NetworkId = #NTID
ORDER BY [Late].SnapshotID ASC
My questions are:
What am I missing?
Have I formatted my query wrong? And the second is why would it return 3.5 hours instead of one given the fact that the second query actually works and returns things properly?
I would have to say that you are missing timezone data.
From your queries, there is no data what timezone the servers are in, or what timezone they are inserting data to.
What exactly do you want?? Do you want the data for the last clock hour? (i.e., if it's 10:37 you want all data between 9:00 and 10:00)
or do you want the data for the past one hour? (i.e., if it's 10:37:12 you want all data between 9:37:13 and 10:37:12)
For the first, change the where clause to
...And NMSDS.SnapshotTimestamp >=
DateAdd(hour, datediff(hour, 0, Current_Timestamp)-1, 0),
And NMSDS.SnapshotTimestamp <
DateAdd(hour, datediff(hour, 0, Current_Timestamp), 0)
For the second, it's easier...
... And NMSDS.SnapshotTimestamp > Current_Timestamp - 1/24
but I admit I'm really confused by the value 6 in the second query... Why a 6, if you are trying for the data for the last single hour ??

Calculating working days including holidays between dates without a calendar table in oracle SQL

Okay, so I've done quite a lot of reading on the possibility of emulating the networkdays function of excel in sql, and have come to the conclusion that by far the easiest solution is to have a calendar table which will flag working days or non working days. However, due to circumstances out of my control, we don't have access to such a luxury and it's unlikely that we will any time in the near future.
Currently I have managed to bodge together what is undoubtedly a horrible ineffecient query in SQL that does work - the catch is, it will only work for a single client record at a time.
SELECT O_ASSESSMENTS.ASM_ID,
O_ASSESSMENTS.ASM_START_DATE,
O_ASSESSMENTS.ASM_END_DATE,
sum(CASE
When TO_CHAR(O_ASSESSMENTS.ASM_START_DATE + rownum -1,'Day')
= 'Sunday ' THEN 0
When TO_CHAR(O_ASSESSMENTS.ASM_START_DATE + rownum -1,'Day')
= 'Saturday ' THEN 0
WHEN O_ASSESSMENTS.ASM_START_DATE + rownum - 1
IN ('03-01-2000','21-04-2000','24-04-2000','01-05-2000','29-05-2000','28-08-2000','25-12-2000','26-12-2000','01-01-2001','13-04-2001','16-04-2001','07-05-2001','28-05-2001','27-08-2001','25-12-2001','26-12-2001','01-01-2002','29-03-2002','01-04-2002','06-04-2002','03-06-2002','04-06-2002','26-08-2002','25-12-2002','26-12-2002','01-01-2003','18-04-2003','21-04-2003','05-05-2003','26-05-2003','25-08-2003','25-12-2003','26-12-2003','01-01-2004','09-04-2004','12-04-2004','03-05-2004','31-05-2004','30-08-2004','25-12-2004','26-12-2004','27-12-2004','28-12-2004','01-01-2005','03-01-2005','25-03-2005','28-03-2005','02-05-2005','30-05-2005','29-08-2005','27-12-2005','28-12-2005','02-01-2006','14-04-2006','17-04-2006','01-05-2006','29-05-2006','28-08-2006','25-12-2006','26-12-2006','02-01-2007','06-04-2007','09-04-2007','07-05-2007','28-05-2007','27-08-2007','25-12-2007','26-12-2007','01-01-2008','21-03-2008','24-03-2008','05-05-2008','26-05-2008','25-08-2008','25-12-2008','26-12-2008','01-01-2009','10-04-2009','13-04-2009','04-05-2009','25-05-2009','31-08-2009','25-12-2009','28-12-2009','01-01-2010','02-04-2010','05-04-2010','03-05-2010','31-05-2010','30-08-2010','24-12-2010','27-12-2010','28-12-2010','31-12-2010','03-01-2011','22-04-2011','25-04-2011','29-04-2011','02-05-2011','30-05-2011','29-08-2011','26-12-2011','27-12-2011')
THEN 0
ELSE 1
END)-1 AS Week_Day
From O_ASSESSMENTS,
ALL_OBJECTS
WHERE O_ASSESSMENTS.ASM_QSA_ID IN ('TYPE1')
AND O_ASSESSMENTS.ASM_END_DATE >= '01/01/2012'
AND O_ASSESSMENTS.ASM_ID = 'A00000'
AND ROWNUM <= O_ASSESSMENTS.ASM_END_DATE-O_ASSESSMENTS.ASM_START_DATE+1
GROUP BY
O_ASSESSMENTS.ASM_ID,
O_ASSESSMENTS.ASM_START_DATE,
O_ASSESSMENTS.ASM_END_DATE
Basically, I'm wondering if a) I should stop wasting my time on this or b) is it possible to get this to work for multiple clients? Any pointers appreciated thanks!
Edit: Further clarification - I already work out timescales using excel, but it would be ideal if we could do it in the report as the report in question is something that we would like end users to be able to run without any further manipulation.
Edit:
MarkBannister's answer works perfectly albeit slowly (though I had expected as much given it's not the preferred solution) - the challenge now lies in me integrating this into an existing report!
with
calendar_cte as (select
to_date('01-01-2000')+level-1 calendar_date,
case when to_char(to_date('01-01-2000')+level-1, 'day') in ('sunday ','saturday ') then 0 when to_date('01-01-2000')+level-1 in ('03-01-2000','21-04-2000','24-04-2000','01-05-2000','29-05-2000','28-08-2000','25-12-2000','26-12-2000','01-01-2001','13-04-2001','16-04-2001','07-05-2001','28-05-2001','27-08-2001','25-12-2001','26-12-2001','01-01-2002','29-03-2002','01-04-2002','06-04-2002','03-06-2002','04-06-2002','26-08-2002','25-12-2002','26-12-2002','01-01-2003','18-04-2003','21-04-2003','05-05-2003','26-05-2003','25-08-2003','25-12-2003','26-12-2003','01-01-2004','09-04-2004','12-04-2004','03-05-2004','31-05-2004','30-08-2004','25-12-2004','26-12-2004','27-12-2004','28-12-2004','01-01-2005','03-01-2005','25-03-2005','28-03-2005','02-05-2005','30-05-2005','29-08-2005','27-12-2005','28-12-2005','02-01-2006','14-04-2006','17-04-2006','01-05-2006','29-05-2006','28-08-2006','25-12-2006','26-12-2006','02-01-2007','06-04-2007','09-04-2007','07-05-2007','28-05-2007','27-08-2007','25-12-2007','26-12-2007','01-01-2008','21-03-2008','24-03-2008','05-05-2008','26-05-2008','25-08-2008','25-12-2008','26-12-2008','01-01-2009','10-04-2009','13-04-2009','04-05-2009','25-05-2009','31-08-2009','25-12-2009','28-12-2009','01-01-2010','02-04-2010','05-04-2010','03-05-2010','31-05-2010','30-08-2010','24-12-2010','27-12-2010','28-12-2010','31-12-2010','03-01-2011','22-04-2011','25-04-2011','29-04-2011','02-05-2011','30-05-2011','29-08-2011','26-12-2011','27-12-2011','01-01-2012','02-01-2012') then 0 else 1 end working_day
from dual
connect by level <= 1825 + sysdate - to_date('01-01-2000') )
SELECT
a.ASM_ID,
a.ASM_START_DATE,
a.ASM_END_DATE,
sum(c.working_day)-1 AS Week_Day
From
O_ASSESSMENTS a
join calendar_cte c
on c.calendar_date between a.ASM_START_DATE and a.ASM_END_DATE
WHERE a.ASM_QSA_ID IN ('TYPE1')
and a.ASM_END_DATE >= '01/01/2012'
GROUP BY
a.ASM_ID,
a.ASM_START_DATE,
a.ASM_END_DATE
There are a few ways to do this. Perhaps the simplest might be to create a CTE that produces a virtual calendar table, based on Oracle's connect by syntax, and then join it to the Assesments table, like so:
with calendar_cte as (
select to_date('01-01-2000')+level-1 calendar_date,
case when to_char(to_date('01-01-2000')+level-1, 'Day')
in ('Sunday ','Saturday ') then 0
when to_date('01-01-2000')+level-1
in ('03-01-2000','21-04-2000','24-04-2000','01-05-2000','29-05-2000','28-08-2000','25-12-2000','26-12-2000','01-01-2001','13-04-2001','16-04-2001','07-05-2001','28-05-2001','27-08-2001','25-12-2001','26-12-2001','01-01-2002','29-03-2002','01-04-2002','06-04-2002','03-06-2002','04-06-2002','26-08-2002','25-12-2002','26-12-2002','01-01-2003','18-04-2003','21-04-2003','05-05-2003','26-05-2003','25-08-2003','25-12-2003','26-12-2003','01-01-2004','09-04-2004','12-04-2004','03-05-2004','31-05-2004','30-08-2004','25-12-2004','26-12-2004','27-12-2004','28-12-2004','01-01-2005','03-01-2005','25-03-2005','28-03-2005','02-05-2005','30-05-2005','29-08-2005','27-12-2005','28-12-2005','02-01-2006','14-04-2006','17-04-2006','01-05-2006','29-05-2006','28-08-2006','25-12-2006','26-12-2006','02-01-2007','06-04-2007','09-04-2007','07-05-2007','28-05-2007','27-08-2007','25-12-2007','26-12-2007','01-01-2008','21-03-2008','24-03-2008','05-05-2008','26-05-2008','25-08-2008','25-12-2008','26-12-2008','01-01-2009','10-04-2009','13-04-2009','04-05-2009','25-05-2009','31-08-2009','25-12-2009','28-12-2009','01-01-2010','02-04-2010','05-04-2010','03-05-2010','31-05-2010','30-08-2010','24-12-2010','27-12-2010','28-12-2010','31-12-2010','03-01-2011','22-04-2011','25-04-2011','29-04-2011','02-05-2011','30-05-2011','29-08-2011','26-12-2011','27-12-2011')
then 0
else 1
end working_day
from dual
connect by level <= 36525 + sysdate - to_date('01-01-2000') )
SELECT a.ASM_ID,
a.ASM_START_DATE,
a.ASM_END_DATE,
sum(c.working_day) AS Week_Day
From O_ASSESSMENTS a
join calendar_cte c
on c.calendar_date between a.ASM_START_DATE and a.ASM_END_DATE
WHERE a.ASM_QSA_ID IN ('TYPE1') and
a.ASM_END_DATE >= '01/01/2012' -- and a.ASM_ID = 'A00000'
GROUP BY
a.ASM_ID,
a.ASM_START_DATE,
a.ASM_END_DATE
This will produce a virtual table populated with dates from 01 January 2000 to 10 years after the current date, with all weekends marked as non-working days and all days specified in the second in clause (ie. up to 27 December 2011) also marked as non-working days.
The drawback of this method (or any method where the holiday dates are hardcoded into the query) is that each time new holiday dates are defined, every single query that uses this approach will have to have those dates added.
If you can't use a calendar table in Oracle, you might be better off exporting to Excel. Brute force always works.
Networkdays() "returns the number of whole working days between start_date and end_date. Working days exclude weekends and any dates identified in holidays."
Excluding weekends seems fairly straightforward. Every 7-day period will contain two weekend days. You'll just need to take some care with the leftover days.
Holidays are a different story. You have to either store them or pass them as an argument. If you could store them, you'd store them in a calendar table, and your problem would be over. But you can't do that.
So you're looking at passing them as an argument. Off the top of my head--and I haven't had any tea yet this morning--I'd consider a common table expression or a wrapper for a stored procedure.

SQL Server - best approach for searching between two dates

Hello and thanks in advance for taking a look at this.
I have discovered an issue in 45 stored procedures that someone else wrote, she commented that the performance has slipped considerably. I took a look and spotted the problem in about 5 minutes, ran a test and went from 60 seconds down to 4 seconds for one of the 45. An index was not being used and a table scan was occurring on a table with 10 million + records. This is using SQL Server 2005.
The table is an audit log and is queried by a stored proc to pull the updt_tmstmp when a record has a specific value. I changed the below block of code to use "NOT IN" vs 8 "product_code <> 'XX' " statements, changed the first datediff to use the indexed column updt_tmstmp and also added the check that the AUDIT_LOG.updt_tmstmp > #dtStartDate to achieve the peformance increase. I just feel this could be implemented differently (more elegantly). I would appreciate any thoughts or ideas on improvements.
WHERE
PRODUCT.product_code NOT IN ('D01', 'D02', 'D03', 'D04', 'D05', 'D06', 'D07', 'D99') AND
AUDIT_LOG.updt_tmstmp >= #dtStartDate AND
--Compares that the date entered is between the two date parameters
(DATEDIFF(dd,GETDATE(),AUDIT_LOG.updt_tmstmp)
BETWEEN DATEDIFF(dd,GETDATE(),#dtStartDate)
AND DATEDIFF(dd,GETDATE(),#dtEndDate))
AND AUDIT_LOG.event_id = (SELECT MIN(AUDIT_LOG.event_id)
FROM L_EVENT_LOG
WHERE AUDIT_LOG.transaction_id = PRODUCT.transaction_id AND AUDIT_LOG.queue = 'AP')
The comparison against audit_log.updt_tmstmp looks a bit strange.
AUDIT_LOG.updt_tmstmp >= #dtStartDate AND
--Compares that the date entered is between the two date parameters
(DATEDIFF(dd,GETDATE(),AUDIT_LOG.updt_tmstmp)
BETWEEN DATEDIFF(dd,GETDATE(),#dtStartDate)
AND DATEDIFF(dd,GETDATE(),#dtEndDate))
I guess this would do the same.
audit_log.updt_tmstmp >= #dtStartDate and
audit_log.updt_tmstmp < #dtEndDate
I have no idea what to do with the correlated sub query. It uses fields from the outer query in the where clause and does not use any fields from l_event_log. You should probably move the where clause to the main query instead.
Perhaps something like this.
where product.product_code not in ( 'D01', 'D02', 'D03', 'D04',
'D05', 'D06', 'D07', 'D99' ) and
audit_log.updt_tmstmp >= #dtStartDate and
audit_log.updt_tmstmp < #dtEndDate and
audit_log.transaction_id = product.transaction_id and
audit_log.queue = 'AP' and
l_event_log.event_id = (select min(audit_log.event_id)
from l_event_log)
I'd chip in with GETDATE() being called three times. Not sure if that gets optimised away, but worth putting that in a variable to start with to see if that helps.

SQL query with week days

I would like to know what is the best way of creating a report that will be grouped by the last 7 days - but not every day i have data. for example:
08/01/10 | 0
08/02/10 | 5
08/03/10 | 6
08/04/10 | 10
08/05/10 | 0
08/06/10 | 11
08/07/10 | 1
is the only option is to create a dummy table with those days and join them altogether?
thank you
Try something like this
WITH LastDays (calc_date)
AS
(SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP) - 6, 0)
UNION ALL
SELECT DATEADD(DAY, 1, calc_date)
FROM LastDays
WHERE DATEADD(DAY, 1, calc_date) < CURRENT_TIMESTAMP)
SELECT ...
FROM LastDays l LEFT JOIN (YourQuery) t ON (l.cal_date = t.YourDateColumn);
Many people will suggest methods for dynamically creating a range of dates that you can then join against. This will certainly work but in my experience a calendar table is the way to go. This will make the SQL trivial and generic at the cost of maintaining the calendar table.
At some point in the future someone will come along and ask for another report that excludes weekends. You then have to make your dynamic days generation account for weekends. Then someone will ask for working-days excluding public-holidays at which point you have no choice but to create a calendar table.
I would suggest you bite the bullet and create a calendar table to join against. Pre-populate it with every date and if you want to think ahead then add columns for "Working Day" and maybe even week number if your company uses a non-standard week-number for reporting
You don't mention the specific language (please do for a more detailed answer), but most versions of sql have a function for the current date (GetDate(), for instance). You could take that date, subtract x (7) days and build your WHERE statement like that.
Then you could GROUP BY the day-part of that date.
select the last 7 transactions and left join it with your query and then group by the date column. hope this helps.