I am generating one time-series from using the below query.
SELECT * from (
select * from generate_series(
date_trunc('hour', '2021-11-13 10:01:38'::timestamp),
'2021-12-13 10:01:38'::timestamp,
concat(480, ' minutes')::interval) as t(time_ent)) as t
where t."time_ent" between '2021-11-13 10:01:38'::timestamp and '2021-12-13 10:01:38'::timestamp
and it will give me output like below.
2021-11-13 18:00:00.000
2021-11-14 02:00:00.000
2021-11-14 10:00:00.000
2021-11-14 18:00:00.000
2021-11-15 02:00:00.000
but I need output like.
2021-11-13 16:00:00.000
2021-11-14 00:00:00.000
2021-11-14 08:00:00.000
2021-11-14 16:00:00.000
2021-11-15 00:00:00.000
currently, the time series hours depend upon the timestamp that I pass. in above it gives me hours like 02,10,18...but I want the hours like 00,08,16...hours should not depend on the time I passed in query. I tried many things but not any success.
as your start of generate_series is set to 10:00:00, so your next step will be 18:00:00
you have to start your serie from 00:00:00 (cast to date) e.g.:
SELECT
time_ent::timestamp without time zone
from (
select * from generate_series(
date_trunc('hour', '2021-11-13 10:01:38'::date),
'2021-12-13 10:01:38'::timestamp ,
concat(480, ' minutes')::interval) as t(time_ent)
) as t
where t."time_ent" between '2021-11-13 10:01:38'::timestamp and '2021-12-13 10:01:38'::timestamp
and the result will be:
2021-11-13 16:00:00.000
2021-11-14 00:00:00.000
2021-11-14 08:00:00.000
2021-11-14 16:00:00.000
2021-11-15 00:00:00.000
2021-11-15 08:00:00.000
Related
I have some dates in postgresql database. I want to find dates from today to next two weeks or 14 days. How i can find the dates between current date and next 14 days? This query is not working.
I have date format 2019-12-26 in database.
"SELECT work_date FROM USERS_SCHEDULE WHERE user_id = 11 AND data(now() +14)";
Simply by adding the number of days to the date you can set the limit date you want.
Sample Data
CREATE TABLE users_schedule (work_date DATE);
INSERT INTO users_schedule
SELECT generate_series(CURRENT_DATE, DATE '2020-01-31', '1 day');
Query (dates between the current date and 3 days later)
SELECT work_date FROM users_schedule
WHERE work_date BETWEEN CURRENT_DATE AND CURRENT_DATE + 3;
work_date
------------
2019-12-26
2019-12-27
2019-12-28
2019-12-29
(4 rows)
If you mean you want to get all possible dates inside an interval, take a look at generate_series:
SELECT generate_series(DATE '2016-08-01', DATE '2016-08-14', '1 day');
generate_series
------------------------
2016-08-01 00:00:00+02
2016-08-02 00:00:00+02
2016-08-03 00:00:00+02
2016-08-04 00:00:00+02
2016-08-05 00:00:00+02
2016-08-06 00:00:00+02
2016-08-07 00:00:00+02
2016-08-08 00:00:00+02
2016-08-09 00:00:00+02
2016-08-10 00:00:00+02
2016-08-11 00:00:00+02
2016-08-12 00:00:00+02
2016-08-13 00:00:00+02
2016-08-14 00:00:00+02
(14 rows)
Using CURRENT_DATE
SELECT generate_series(CURRENT_DATE, DATE '2019-12-31', '1 day');
generate_series
------------------------
2019-12-26 00:00:00+01
2019-12-27 00:00:00+01
2019-12-28 00:00:00+01
2019-12-29 00:00:00+01
2019-12-30 00:00:00+01
2019-12-31 00:00:00+01
(6 rows)
SELECT work_date
FROM users_schedule
WHERE user_id = 11
AND work_date BETWEEN CURRENT_DATE
AND CURRENT_DATE + INTERVAL '14 days'
I have a record as below
Login Time Logout Time User Shift
2017-04-01 21:30:00.000 2017-04-02 00:00:00.000 I0402 N
2017-04-02 00:00:00.000 2017-04-02 00:30:00.000 I0402 N
2017-04-02 01:30:00.000 2017-04-02 05:30:00.000 I0402 N
2017-04-02 06:30:00.000 2017-04-02 08:30:00.000 I0402 N
I want to get the earliest entry time and latest logout time for the user. How I can do this?
The record I wish to as below
Login Time Logout Time User Shift
2017-04-01 21:30:00.000 2017-04-02 08:30:00.000 I0402 N
Is it possible to do that?
Thanks.
Edit
I try before with Max and Min and group by User and Shift, it is work. If it is more than 1 days record, it seem like not work
2017-04-01 21:30:00.000 2017-04-02 00:00:00.000 I0402 N
2017-04-02 00:00:00.000 2017-04-02 00:30:00.000 I0402 N
2017-04-02 01:30:00.000 2017-04-02 05:30:00.000 I0402 N
2017-04-02 06:30:00.000 2017-04-02 08:30:00.000 I0402 N
2017-04-02 21:30:00.000 2017-04-03 00:00:00.000 I0402 N
2017-04-03 00:00:00.000 2017-04-03 00:30:00.000 I0402 N
2017-04-03 01:30:00.000 2017-04-03 05:30:00.000 I0402 N
The expected result as below
2017-04-01 21:30:00.000 2017-04-02 08:30:00.000 I0402 N
2017-04-02 21:30:00.000 2017-04-03 05:30:00.000 I0402 N
SELECT
User
,MIN(Login Time)
,MAX(Logout Time)
,Shift
FROM
dbo.table
GROUP BY
User,Shift
I try before with Max and Min and group by User and Shift, it is work. If it is more than 1 days record, it seem like not work
2017-04-01 21:30:00.000 2017-04-02 00:00:00.000 I0402 N
2017-04-02 00:00:00.000 2017-04-02 00:30:00.000 I0402 N
2017-04-02 01:30:00.000 2017-04-02 05:30:00.000 I0402 N
2017-04-02 06:30:00.000 2017-04-02 08:30:00.000 I0402 N
2017-04-02 21:30:00.000 2017-04-03 00:00:00.000 I0402 N
2017-04-03 00:00:00.000 2017-04-03 00:30:00.000 I0402 N
2017-04-03 01:30:00.000 2017-04-03 05:30:00.000 I0402 N
The expected result as below
2017-04-01 21:30:00.000 2017-04-02 08:30:00.000 I0402 N
2017-04-02 21:30:00.000 2017-04-03 05:30:00.000 I0402 N
You can use below query to get the result,
SELECT T1.USER, T2.MAX_LOGIN_TIME, T3.MIN_LOGIN_TIME FROM
TABLE_NAME T1
INNER JOIN (SELECT MAX(LOGIN_TIME) AS MAX_LOGIN_TIME,USER FROM TABLE_NAME
GROUP BY USER) T2
ON (T1.USER = T2.USER)
INNER JOIN (SELECT MIN(LOGIN_TIME) AS MIN_LOGIN_TIME,USER FROM TABLE_NAME
GROUP BY USER) T3
ON (T1.USER = T3.USER);
It seems what you really want is one result row per day, user and shift. So group by these and take min and max time.
select min(Login_time) as login, max(logout_time) as logout, user, shift
from mytable
group by date(login_time), user, shift
order by date(Login_time), user, shift;
Here DATE(login_time) is just an example. How to get the date from a datetime differs from DBMS to DBMS. Look up date/time functions for your DBMS to get the appropriate function.
I am having a Database in which data is been logged in regular interval of time i e for 5 minutes say it is been logged for 24 hours as shown in below table.
Date and Time Value
2016-09-17 14:00:00 25.26
2016-09-17 14:05:00 24.29
2016-09-17 14:10:00 25.22
2016-09-17 14:20:00 25.10
2016-09-17 23:55:00 20.21
I want To display Every 1 hour reading using SQL query There are chances the some reading may be missing The expected Output should be.
Date and Time Value
2016-09-17 14:00:00 25.26
2016-09-17 15:00:00 27.29
2016-09-17 16:00:00 28.12
2016-09-17 17:00:00 22.11
There are chances my be that some reading may be missing. like
Date and Time Value
2016-09-17 14:35:00 25.26
This reading may be missing
Please Suggest SQL query for the same
SELECT t1.DateCol,
t1.Value
FROM yourTable t1
INNER JOIN
(
SELECT MIN(DateCol) AS firstDate
FROM yourTable
GROUP BY FORMAT(DateCol, 'dd/MM/yyyy hh')
) t2
ON t1.DateCol = t2.firstDate
If you instead wanted to group by every 15 minutes, you could try:
GROUP BY CONCAT(FORMAT(DateCol, 'dd/MM/yyyy hh'),
FLOOR(DATEPART(MINUTE, DateCol) / 15))
I have the following data set in the test table:
create table test
(
columndate date,
columntime datetime
)
insert into test values('2014-01-01','22:00:00')
insert into test values('2014-01-02','06:00:00')
insert into test values('2014-01-03','23:00:00')
insert into test values('2014-01-04','05:00:00')
insert into test values('2014-02-01','10:00:00')
insert into test values('2014-02-01','13:00:00')
insert into test values('2014-02-01','15:00:00')
insert into test values('2014-02-01','05:00:00')
columndate columntime
------------------------------------
2014-01-01 1900-01-01 22:00:00.000
2014-01-02 1900-01-01 06:00:00.000
2014-01-03 1900-01-01 23:00:00.000
2014-01-04 1900-01-01 05:00:00.000
2014-02-01 1900-01-01 10:00:00.000
2014-02-01 1900-01-01 13:00:00.000
2014-02-01 1900-01-01 15:00:00.000
2014-02-01 1900-01-01 05:00:00.000
Now I want to show only night timing in the result for example:
columndate columntime
-----------------------------------
2014-01-01 1900-01-01 22:00:00.000
2014-01-02 1900-01-01 06:00:00.000
2014-01-03 1900-01-01 23:00:00.000
2014-01-04 1900-01-01 05:00:00.000
2014-02-01 1900-01-01 05:00:00.000
For which I am trying the following script:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '06:00:00' and '23:00:00'
Note: I will not get the record of timing 05:00:00
But when I use the following script:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '05:00:00' and '23:00:00'
Note: I will get expected result but I am getting record of timing 06:00:00 also which I don't want to show.
How can I fix it?
You were really close: instead of BETWEEN you need to use a pair of >= and <=, because you need times outside an interval:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and (cast(columntime as time) <= '05:00:00' OR cast(columntime as time) >= '23:00:00')
Demo.
Does this meet your needs??:
select * from test
where columndate between '2014-01-01' and '2014-02-01'
and cast(columntime as time) between '05:00:00' and '23:00:00' and columntime ! ='06:00:00'
I have nearly 15000 data rows with the first column containing date in the format:
2012-05-10 09:00:00.000
I need this data to be sorted by year then month, then day, then hour so for example:
2012-05-10 09:00:00.000
2012-05-10 10:00:00.000
2012-05-10 11:00:00.000
2012-05-10 12:00:00.000
2012-05-11 09:00:00.000
2012-05-11 10:00:00.000
2012-05-11 11:00:00.000
2012-05-11 12:00:00.000
2012-06-01 02:00:00.000
2012-06-01 03:00:00.000
2012-06-01 04:00:00.000
2012-06-01 05:00:00.000
Current SQL Query to do this is below:
SELECT MIN(Datetime)
GROUP BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime)
HAVING MIN(jmusa_LOG1.DateTime) NOT IN(SELECT DateTime FROM AverageRawData)
ORDER BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime)
You are describing a normal date sort, so you can just do:
select MyDate
from AverageRawData
order by MyDate
If you don't want duplicates, add DISTINCT like this:
select distinct MyDate
from AverageRawData
order by MyDate
If this does not meet your requirements, please provide sample data used to generate your output example.