How can I group by date time column for custom hour - sql

How can I group by date time column for custom hour, for example every 5 Hour or every 12 Hour or every 24 hour.
Select CreateOn, Count(*) From Table1
Group By ?????

You should select start date/time, '2011-01-01' in this example and then use DATEDIFF() function to get time difference in hours between start date and CreateOn. Then use / operator to get an integer interval number (5 hours in this example).
Select
min(DATEADD(HOUR,DATEDIFF (HOUR,'2011-01-01',CreateOn )/5*5,'2011-01-01'))
as Start_time,
max(DATEADD(HOUR,(DATEDIFF (HOUR,'2011-01-01',CreateOn )/5+1)*5,'2011-01-01'))
as End_Time,
DATEDIFF (HOUR,'2011-01-01',CreateOn )/5 as Interval_Number,
Count(*) as _Count
From Table1
Group By DATEDIFF (HOUR,'2011-01-01',CreateOn )/5

If I understand your question correctly, you could try something like this:
SELECT CreateOn, Count(*) FROM Table1 GROUP BY (DATEPART(MINUTE, [Date]) % 12)

Related

Window function for average

I have this table timestamp_table and I'm using Presto SQL
timestamp | id
2021-01-01 10:00:00 | 2456
I would like to compute the number of unique IDs in the last 24 and 48 hours and I thought this could be achieved with window functions but I'm struggling. This is my proposed solution, but it needs work
SELECT COUNT(id) OVER (PARTITION BY timestamp ORDER BY timestamp RANGE BETWEEN INTERVAL '24' HOUR PRECEDING AND CURRENT ROW)
You're probably having trouble due to the PARTITION BY clause, since the COUNT will only apply to rows within the same timestamp values.
Try something like this, as a starting point:
The fiddle
SELECT *
, COUNT(id) OVER (ORDER BY timestamp RANGE BETWEEN INTERVAL '24' HOUR PRECEDING AND CURRENT ROW)
, MIN(id) OVER (ORDER BY timestamp RANGE BETWEEN INTERVAL '24' HOUR PRECEDING AND CURRENT ROW)
FROM tbl
;
I think that you can't get data for both time intervals by one table scan. Because row that is in last 24 hours must be in both groups: 24 hours and 48 hours. So you must do 2 request or union them.
select 'h24', count(distinct id)
from timestamp_table
where timestamp < current_timestamp and timestamp >= date_add(day, -1, current_timestamp)
union all
select 'h48', count(distinct id)
from timestamp_table
where timestamp < current_timestamp and timestamp >= date_add(day, -2, current_timestamp)

Postgresql Distinct Statement

How can i get the minutes distinct value with timestamp ...
Like , if table contains 1 minute 100 records are there...so i want count of records present or not per minute ...
For example,
SELECT DISTINCT(timestamp) FROM customers WHERE DATE(timestamp) = CURRENT_DATE
Result should be ..like
timestamp record
30-12-2019 11:30 5
30-12-2019 11:31 8
One option would be ::date conversion for timestamp column including GROUP BY :
SELECT timestamp, count(*)
FROM tab
WHERE timestamp::date = current_date
GROUP BY timestamp
Demo for current day
timestamp::date might be replaced with date(timestamp) like in your case.
Update : If the table contains data with precision upto microseconds, then
SELECT to_char(timestamp,'YYYY-MM-DD HH24:MI'), count(*)
FROM tab
WHERE date(timestamp) = current_date
GROUP BY to_char(timestamp,'YYYY-MM-DD HH24:MI')
might be considered.
Try something like the following:
SELECT DATE_TRUNC('minute', timestamp) as timestamp, COUNT(*) as record
FROM customers
WHERE DATE(timestamp) = CURRENT_DATE
GROUP BY DATE_TRUNC('minute', timestamp)
ORDER BY DATE_TRUNC('minute', timestamp)

How to get hour by hour data in sql server (specific with minutes)

how to get hour by hour data (no matter what is the date) in sql server, I know it can be achieved with datePart(hour, columnname) but to be specific, I need data for specific intervals including minutes regardless of dates.
Scenario: 'TestTable' contains column - DBTimestamp with data type (DateTime)
I need all Records from 'TestTable' for which 'DBTimestamp' is between 3 Hours 35 Minutes And 4 Hours 30 Minutes regardless of date specified.
You can use this.
SELECT * FROM TestTable WHERE CAST(DBTimestamp AS TIME) BETWEEN '03:35' AND '04:30'
From my understanding you need records that in specific range of time regardless of date part. So I've come up with following solution:
SELECT * FROM (SELECT Id, CAST(Time AS time) [time]
FROM Table) AS Q1
WHERE Q1.time > CAST('03:35:00' AS time)
AND Q1.time < CAST('12:30:00' AS time)
Here is SQL Fiddle:
http://sqlfiddle.com/#!6/21b313/1
Well I guess you could do some variation of the following:
SELECT *
FROM TABLE
WHERE (DATEPART(HOUR, TIMESTAMP) = 3 AND DATEPART(MINUTE, TIMESTAMP) >= 35) OR
(DATEPART(HOUR, TIMESTAMP) = 4 AND DATEPART(MINUTE, TIMESTAMP) <= 30)

Query to produce data only within the asked hour range

Oracle SQL:
I been trying to get this some snippet of query working. When you run it, it prompts you for an hour, and displays no records. I don’t want the query to have a static hour as it will need to be run 4 times a day.
So from the ‘&date’ input, I want it to show data for the past 24 hours. Is that possible?
dt_time = timestamp(6) field
select distinct to_char(dt_ time,'dd/mm/yyyy hh24'), fault_description
from order
where to_char(dt_time,'hh24') <= '&date' -24
order by to_char(dt_ time,'dd/mm/yyyy hh24');
Example, if you enter 10 (when query executed) it will show the data from 10(:00) through to 10(:00)next day
[Hope there is enough info for someone to answer, please]
Use a combination of between and interval:
where dt_time between
to_date(&date, 'ddmmyyyy hh24') and
to_date(&date, 'ddmmyyyy hh24') + interval '24' hour
You'll need to pass/parse the input date as a full date and not only the hour part, in order to prevent unexpected results.
You can use NUMTODSINTERVAL in a CTE to get the hour as user input.
WITH t_hour( h ) AS
( SELECT NUMTODSINTERVAL(&d,'HOUR' ) h FROM DUAL
)
SELECT DISTINCT TO_CHAR(dt_time,'dd/mm/yyyy hh24'),
fault_description
FROM ORDER
CROSS JOIN t_hour
WHERE dt_time BETWEEN TRUNC(SYSDATE) + h AND TRUNC(SYSDATE) + 1 + h
ORDER BY 1;
You need to make use of SYSDATE as you are fetching last one day data.
Also if you add or subtract number with a date column the offset will be number of days, not number of hours. So you need to use &date/24
Try this:
select distinct to_char(dt_time,'dd/mm/yyyy hh24'), fault_description
from order
where dt_time - &date / 24 between TRUNC(sysdate-1) and TRUNC(sysdate)
order by to_char(dt_ time,'dd/mm/yyyy hh24');
Please comment.
WHERE DATEDIFF(SYSDATE,DT_TIME) * 24 < &DATE AND DT_TIME < SYSDATE

Count DateTime Stamp For Results

I have a table containing a list of userids and a datetime stamp for when a transaction is carried out. It looks like the following:
UserId | Stamp
John11| 01/01/2013 01:15:27
I would like to return a query which shows for a period of time how many transactions each user has done.
I have tried a count but due to the time part of the stamp it counts for each individual millisecond rather than overall.
Thanks in advance
Use time_to_sec function for get actual value.
Select count(UserId), UserId from table where time_to_sec(stamp) between
fromTime and toTime
-- fromTime and toTime are interval period
Your question does not specify what period you want but here is an example for month which you could adapt to other periods:
SELECT
(DATEPART(YEAR, Stamp) * 100) + RIGHT('0' + DATEPART(MONTH, Stamp), 2) AS month_group
,COUNT(*) AS count
FROM MyTable
GROUP BY
(DATEPART(YEAR, Stamp) * 100) + RIGHT('0' + DATEPART(MONTH, Stamp), 2)
Or for one period only use;
SELECT
COUNT(*) AS count
FROM MyTable
WHERE Stamp BETWEEN ? AND ?
select user_id,count(userid) cnt from table
where stamp between to_timestamp('01-01-2013 01:15:27', 'DD-MM-YYYY HH:MI:SS') and
to_timestamp('03-01-2013 12:15:27', 'DD-MM-YYYY HH:MI:SS')
group by user_id
Depending on how you want to do the count, something like this should work.
Every unique occurence of dd-mon-yyyy hh24:mi:ss for a userid is counted.
SELECT userid
, count(distinct TO_CHAR(stamp, 'dd-mon-yyyy hh24:mi:ss'))
FROM MYTABLE
where stamp > to_date('16-03-2013','DD-MM-YYYY')
group by userid
Live example: http://sqlfiddle.com/#!4/71241/1