Sql express time Query - sql

am working with MS SQL express and Ignition SCADA by http://www.inductiveautomation.com/
In the SCADA package you are able to create tags from SQL query's. I am trying to use SQL tags to calculate the average packages per minute in a 30min time frame. I was able to do this with two tags and an expression
SELECT MAX(L8Total)
FROM Slicing_tot
WHERE t_stamp BETWEEN DATEADD(minute, -30, GETDATE()) AND GETDATE()
 
SELECT MIN(L8Total)
FROM Slicing_tot
WHERE t_stamp BETWEEN DATEADD(minute, -30, GETDATE()) AND GETDATE()
What I would like to do from here is store the expressions value and find the max and average for the last 30 days based on time. But I have no idea how to filter 30days of information at a certain time
IE what was the max packages per minute we had at 10:30 from the last 30 days
IE what was the average packages per minute we had at 11:45 form the last 30 days
Please keep in mind that I am new to SQL

SELECT DATEPART(MINUTE, t_stamp)
,MAX(L8Total)
,MIN(L8Total)
FROM Slicing_tot
WHERE ( CONVERT(DATE, t_stamp) >= CONVERT(DATE, GETDATE() - 30)
AND CONVERT(DATE, t_stamp) <= CONVERT(DATE, GETDATE())
)
AND ( CONVERT(TIME, #variable) >= '22:30'
AND CONVERT(TIME, #variable) <= '23:00'
)
GROUP BY DATEPART(MINUTE, t_stamp)
GETDATE()-30 will get you datetime of today minus 30 days ago. Since you are working with datetime field it is best to convert it to date to make sure that you get correct date range. Use of >= and <= is better than between because you is always clear what you doing. Read #Aaron's blog
than for the second part just convert your datetime column to time to limit to specific range during the day.

The following would select between 10 & 11 AM over those 30 days
SELECT MIN(L8Total)
FROM Slicing_tot
WHERE t_stamp BETWEEN DATEADD(dd, -30, GETDATE()) AND GETDATE()
and Datepart(hh,t_stamp) between 10 and 11
or you could compare the time part of the t_stamp to time
SELECT MIN(L8Total)
FROM Slicing_tot
WHERE t_stamp BETWEEN DATEADD(dd, -30, GETDATE()) AND GETDATE()
and convert(time, t_stamp) between '10:30:00.000' and '10:31:00.000'
which would give you the results between 10:30 and 10:31 inclusive of the end points over the last 30 days.

Related

Get last 7 days data from today 7am to last 7 days 7am

I have a query that currently can view the timestamp between yesterday 7am and today 7am data. How do I extract the timestamp of last 7 day timestamp. For example, if today is 10th August 2022, I would like to see the data of 3rd August 7am to 10th August 7am data. This is my current SQL query:
select TOP (10000000) id, PartNum, TimeStamp,Station
from test_module
where TimeStamp >= '2022-07-16 07:00:00'
and Timestamp <= '2022-07-17 07:00:00'
order by TimeStamp asc
datetimes can participate in addition. Thus, you can construct the day parts(remove the time first), and then add 7 hours as a time:
declare #start datetime = dateadd(day,-6,convert(datetime,(convert(date,getdate()))) + convert(datetime,convert(time,'07:00:00'))
declare #end datetime = dateadd(day, 0,convert(datetime,(convert(date,getdate()))) + convert(datetime,convert(time,'07:00:00'))
Be careful tossing the term "timestamp" around in SQL Server, it could lead to...miscommunications...
Timestamp >= dateadd(hour, 7, dateadd(day, -7, convert(datetime,
convert(date, getdate()))))
and Timestamp <= dateadd(hour, 7, convert(datetime, convert(date,
getdate())))
Can use getdate() function and for last 7 days, -7
For instance,
where DateCol between DateAdd(DD,-7,GETDATE() ) and GETDATE()
Try dateadd
See also thread here

SQL : Get yesterdays day with a certain timestamp

i am trying to get yesterdays date in SQL SERVER with a certain timestamp.
I know how to get yesterdays date in SQL without the timestamp using the following query :
Which gives me every startdate starting from '00:00:00':
SELECT se.startdate
FROM sessions se
WHERE se.startdate >= DATEADD(day, -1, CAST(GETDATE() AS date))
Instead I want to get the yesterdays date with a certain timestamp.
For this example every startdate starting from '06:00:00'(AM). Like shown below:
SELECT se.startdate
FROM sessions se
WHERE se.startdate >= '05-09-2022 06:00:00'
If I do it the way it was shown in the second example I would have to change the day manually, which would obviously repetitive.
Is there a way to combine the first example with second one so that we get always yesterdays date at
'06:00:00' ?
(OR ANY GIVEN TIME )
Might as well add another solution, DATETIMEFROMPARTS:
DECLARE #Yesterday date = DATEADD(DAY, -1, GETDATE());
SELECT DATETIMEFROMPARTS(YEAR(#Yesterday),MONTH(#Yesterday),DAY(#Yesterday),6,0,0,0);
For example 06:20:40 of the previous day:
select DATEADD(second, (6 * 60 + 20 ) * 60 + 40, cast(DATEADD(day, -1, cast(GETDATE() AS date)) as datetime)) t
you can do it this way:
SELECT DATEADD(day, DATEDIFF(day, 1, GETDATE()), '06:00:00')

Filtering in SQL between evening previous day and morning current day

I am trying to filter a table between 4 pm of previous day and 4am of current day but am at loss of how to query that.
Something like:
WHERE
dateColumn >= DATEADD(DAY, -1, GETDATE()) AND dateColumn <= GETDATE()
AND DATEPART(hh, dateColumn) >= 16 AND DATEPART(hh, dateColum) <= 4
I realize the second line in WHERE statement is obviously incorrect and will not return any results but that is to give an idea of what I am trying to do. All help is appreciated!
There are various ways you could do this, this gets the datetime at 1600 yesterday and 1600 today.
WHERE dataColumn >= dateadd(hour, -8, convert(datetime, convert(date, getdate())))
AND dateColumn <= dateadd(hour, 16, convert(datetime, convert(date, getdate())));

Get last 90 days records (SQL)

I have the following query to get the last 90 days records from my DB.
Sample Data below:
my query code:
SELECT
Email
,Country
,Date_of_Birth
,Date_Added
,Received_ProfileCompletionPromoCode
,First_Name
,Purchase_since_entry
,Exit_Date
FROM
Profile_Completion_Journey_Exit_Log
WHERE
Exit_Date >= DATEADD(d, -90, GETDATE())
But I am getting the result where Exit_Date is 10/11/2020. What would be my error here?
Your code gives you records whose date is not older than 90 days ago.
If you want records whose date is exactly 90 days ago, then:
WHERE Exit_Date = DATEADD(day, -90, CONVERT(DATE, GETDATE())
The conversion to date is an important step. GETDATE() returns the current date and time: we need to truncate the time part.
This assumes that Exit_Date is of date datatype. If it has a time component, then:
WHERE Exit_Date >= DATEADD(day, -90, CONVERT(DATE, GETDATE())
AND Exit_Date < DATEADD(day, -89, CONVERT(DATE, GETDATE())
If you want data from 90 days ago, then use:
WHERE Exit_Date >= DATEADD(day, -90, CONVERT(DATE, GETDATE())) AND
Exit_Date < DATEADD(day, -89, CONVERT(DATE, GETDATE()))
This gets results from exactly 90 days ago.
Note the conversion to DATE. Despite its name, GETDATE() has a time component.

Datediff GETDATE Add

In this type of code,
AND Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -6), 0)
It supposed to pull records with the date 6 days ago, until today. How can I make it pull records from 7 days ago until yesterday?
I know changing -6 to -7 will pull records from 7 days ago, but which variable is the end of the date span so I can change it to -1?
It's not a date span.
The condition you have there is really only one condition: greater than. The right side of the greater than is 6 days ago, so your condition matches any date that is later than the date six days ago. In other words, it doesn't stop at Today; it includes tomorrow, next week, and next year, too.
AND ( Orders.ShipDate >= DATEADD(Day, Datediff(Day,0, GetDate() -7), 0)
AND Orders.ShipDate < DATEADD(Day, Datediff(Day,0, GetDate()), 0) )
That's what you really want. It matches dates which are later than midnight of the day 7 days ago, and dates which are before midnight today (which is any time yesterday).
The "end of the date span" isn't in your query.
Change your code to:
AND (Orders.ShipDate BETWEEN DATEADD(Day, -1, GetDate()) AND DATEADD(Day, -7, GetDate()))
This should work too and it eliminates the needless addition of 0 days:
select *,DATEDIFF(Day, orders.ShipDate, GETDATE()) AS DAYS_SINCE_TODAY
from Orders
where DATEDIFF(Day, orders.ShipDate, GETDATE()) >= 1 AND --This many days since today
DATEDIFF(Day, orders.ShipDate, GETDATE()) <= 7 --Going back this many days
I like the BETWEEN function. This may be a different flavor of SQL than you are using, but this is what I use.
BETWEEN dateadd(second, 0, dateadd(dd,datediff(dd,7,getdate()),0)) AND dateadd(second,-1, dateadd(dd,datediff(dd,0,getdate()),0))