Get records from last hour
try this !!
SELECT Field1, OrderFor, Writeback, Actshipdate, Orderstatus, receivedate, receivetime
FROM orderinfo, shippinginfo
WHERE orderinfo.orderid = shippinginfo.orderid
AND shippinginfo.custid = '37782'
AND receivedate = DATE(NOW())
AND receivetime > DATEADD(HOUR, -1, GETDATE())
Get rows of Last hour, Last 'x' minutes in SQL Server
Your time expression is:
WHERE Time_picked BETWEEN DATEADD(HOUR, -1, GETDATE()) AND CAST(GETDATE() AS DATE)
Under most circumstances, the first will be larger then the second, because the CAST()
removes the time component. I suspect you want:
WHERE Time_picked BETWEEN DATEADD(HOUR, -1, GETDATE()) AND GETDATE()
Or assuming that all time values are in the past:
WHERE Time_picked >= DATEADD(HOUR, -1, GETDATE())
Find records produced in the last hour
Use this:
SELECT *
FROM Whatever
WHERE myTime > DATEADD(HOUR, -1, GETDATE())
Sql Query to find data only from last hour
Use DATE_SUB and NOW() functions in query
SELECT count(*) as lasthour_count
FROM user_otp
WHERE datetime >= DATE_SUB(NOW(),INTERVAL 1 HOUR);
Extract the record for last hour for specific date
If you just want the number of tickets in the last hour on a given day:
DECLARE @date date = '20220330';
SELECT COUNT(*)
FROM dbo.PS_TKT_HIST
WHERE BUS_DAT = @date
AND TKT_DAT >= DATEADD(HOUR, 23, CONVERT(datetime2, @date));
For any hour other than the last hour (let's say, the 9PM hour):
WHERE BUS_DAT = @date
AND TKT_DAT >= DATEADD(HOUR, 21, CONVERT(datetime2, @date))
AND TKT_DAT < DATEADD(HOUR, 22, CONVERT(datetime2, @date));
If by "last hour" you don't mean 11 PM but rather the last hour there was a sale, you would have to do something like this:
DECLARE @date date = '20220330';
SELECT TOP (1) COUNT(*)
FROM dbo.PS_TKT_HIST
WHERE BUS_DAT = @date
GROUP BY DATEPART(HOUR, TKT_DAT)
ORDER BY DATEPART(HOUR, TKT_DAT) DESC;
Query to find out the exact last hour data in Postgresql
The main issue is your GROUP BY
statement. time
is unique across all values, but so is time
combined with data
. The group needs to specify the common value that all items in the group share. We can do that by truncating the time to the hour, then group by that.
The following shows the averages for just the last full hour:
SELECT date_trunc('hour', time) as Hour, avg(data) as Avg
FROM exercise
WHERE time >= date_trunc('hour', now()) - INTERVAL '1 hour'
AND time < (date_trunc('hour', now()))
GROUP BY date_trunc('hour', time);
For all hours except the current partial hour just use this filter:
WHERE time < (date_trunc('hour', now()))
UPDATE:
The data example from OP has changed significantly.
The following fiddle demonstrates a fictious example dataset that is used for the explanation that follows:
https://www.db-fiddle.com/f/rqZ5rsgKFJBKufPsxmFzFX/3
Query #1
Show the averages of the data values for each hour of the day. We are using DATE_TRUNC
to group the readings and compute the average, this implementation will consider the following arbitrary groupings:
- Group 01:00:
01:00, 01:01, 01:59 - Group 02:00:
02:00, 02:01, 02:59
SELECT date_trunc('hour', time) as Hour, avg(data) as Avg
FROM exercise
GROUP BY date_trunc('hour', time);
hour | avg |
---|---|
2022-01-02T01:00:00.000Z | 1.57 |
2022-01-02T00:00:00.000Z | 1.2999999999999998 |
2022-01-02T02:00:00.000Z | 1.275 |
How to get Last two hours Records From SQL Server
It seems that Time is a TEXT field.
You should consider to combine Date and Time to get a DateTime field, and then use it in the WHERE clause.
This answer in SO could help: https://stackoverflow.com/a/7289875/3270427
SELECT *
FROM Report
WHERE CAST(Date AS DATETIME) + CAST(Time AS DATETIME) >= DATEADD(HOUR, -2, GETDATE());
Fetching rows added last hour
Make use of the DATE_SUB()
and NOW()
functions:
select count(*) as cnt
from log
where date >= DATE_SUB(NOW(),INTERVAL 1 HOUR);
Hope it helps you : )
Related Topics
How to Update in SQLite Using a Left Join to Select Candidate Rows
Sql: Select Maximum Value for Each Unique Key
In Ms SQL Server, How to "Atomically" Increment a Column Being Used as a Counter
Delete the 'First' Record from a Table in SQL Server, Without a Where Condition
Differencebetween a Hash Join and a Merge Join (Oracle Rdbms )
How to Quote Values Using Group_Concat
Managing Hierarchies in SQL: Mptt/Nested Sets VS Adjacency Lists VS Storing Paths
SQL Server:Get All String Occurences (Tags) from Nvarchar(Max) Variable Containing a JSON String
SQL Server Procedure Declare a List
Oracle Insert via Select from Multiple Tables Where One Table May Not Have a Row
Select Something That Has More/Less Than X Character
Join Four Tables Involving Left Join Without Duplicates
Mongodb and Postgresql Thoughts
How to Group Nearby Latitude and Longitude Locations Stored in SQL