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
Related
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)
I'm trying to SELECT all the rows from a SQL database which are between an hour interval, for every day.
The datetime column is called "Dt" and has the following datetime format: 2019-10-17 16:03:43
I'd like to extract all the rows from this table where the Dt was between 22:00:00 and 02:00:00, for everyday.
SELECT *
FROM MY_TABLE
WHERE "Dt" BETWEEN '*-*- 22:00:00' AND '*-*- 02:00:00';
where * should be any...
Thanks for your support!
EDIT: I forgot to mention: I'm using the integrated SQL interpreter from DB Browser for SQLite
You need to extract the time part of the date and compare that it is within the range. Since midnight is between 22 and 2, you will need to split it to two comparisons, time between 22 and 0 and between 0 and 2.
To see how to extract the time take a look at this question.
With Postgres, assuming dt is defined as timestamp you can do the following:
SELECT *
FROM MY_TABLE
WHERE "Dt" BETWEEN "Dt"::date + time '22:00:00' and ("Dt"::date + 1) + time '02:00:00'
Or if you want to exclude timestamps at precisely 02:00:00
SELECT *
FROM MY_TABLE
WHERE "Dt" >= "Dt"::date + time '22:00:00'
and "Dt" < ("Dt"::date + 1) + time '02:00:00'
select DT_time from (
select cast (substr(to_char(Dt,'dd-mm-yyyy HH:MM:SS'),12,2) as integer ) as DT_time from MY_TABLE )
where DT_time between 2 and 22;
between 22:00:00 and 02:00:00
means:
SELECT *
FROM MY_TABLE
WHERE
substr(Dt, 12) BETWEEN '22:00:00' AND '23:59:59'
OR
substr(Dt, 12) BETWEEN '00:00:00' AND '02:00:00'
This will work ::
SELECT *
FROM MY_TABLE
WHERE DATEPART(HOUR, Dt)>22
AND DATEPART(HOUR, Dt)<2
Update :
SELECT *
FROM MY_TABLE
WHERE Dt Between DATEADD (hour,22,DATEADD(day, DATEDIFF(day, 0, Dt), 0)) AND DATEADD (hour,2,DATEADD(day, DATEDIFF(day, -1, Dt), 0))
SELECT *
FROM MY_TABLE
WHERE DATEPART(HOUR, Dt)>22
OR DATEPART(HOUR, Dt)<2
Above query work for you..
1st one will check only for particular date and consecutive next date along with your time range.
But If you don't care about dates and only looking for time interval in particular hours then 2nd one is for you.
For SQLite :
SELECT *
FROM MY_TABLE
WHERE strftime('%H','Dt')>22
OR strftime('%H','Dt')<2
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)
Here is my below query which gives the count and group by each hour
SELECT ADD_SECONDS(start_time,- MINUTE(start_time) * 60 - SECOND(start_time)) as time , to_integer(to_varchar(start_time, 'DD')) as day , count(*) as count FROM SYSTEM.TABLE where start_time >= '2016-01-01 00:00:00' and start_time <= '2016-01-01 23:59:59' and place_id=1 group by ADD_SECONDS(start_time,- MINUTE(start_time) * 60 - SECOND(start_time)),to_integer(to_varchar(start_time, 'DD'))
order by ADD_SECONDS(start_time,- MINUTE(start_time) * 60 - SECOND(start_time))
But at the time period 11:00 pm to 12:00 pm I have no count so instead of not returning the row I want it to return the row with 0.
So when I went through some search I found that COALESCE can help so I tried with
SELECT COALESCE (( SELECT ADD_SECONDS(start_time,- MINUTE(start_time) * 60 - SECOND(start_time)) as time , to_integer(to_varchar(start_time, 'DD')) as day , count(*) as count FROM SYSTEM.TABLE where start_time >= '2016-01-01 00:00:00' and start_time <= '2016-01-01 23:59:59' and place_id=1 group by ADD_SECONDS(start_time,- MINUTE(start_time) * 60 - SECOND(start_time)),to_integer(to_varchar(start_time, 'DD'))
order by ADD_SECONDS(start_time,- MINUTE(start_time) * 60 - SECOND(start_time))
), 0);
But it did not also work. Any help is appreciated.
In order to 'have a row' for every hour, you need to create the hour-rows independent of your data. This could be done e.g. by using an auxiliary table that has a row for every hour.
SAP HANA provides shared default aux. tables that can be used for that purpose (these tables also facilitate date/time conversion via lookup): M_TIME_DIMENSION... . SAP HANA Docu M_TIME_DIMENSION
Just select the range of time values you are interested in from these tables and outer join it with your aggregated actual values.
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)