Get the dates of two weeks from today from database - sql

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'

Related

Postgres generate_series how to exclude last day when hour is 00:00

I need to generate a series of days in postgresql that would produce different result depending on the hours in the timestamp.
My series generation works fine when the time is not midnight.
For time range 2023-01-06 10:00:00+00 - 2023-02-03 10:00:00+00 I get a list of days where the first element is 2023-01-06 and the last is 2023-02-03. This works as expected:
generate_series('2023-01-06 10:00:00+00'::date, '2023-02-03 10:00:00+00'::date, '1 day')
However, for time range 2023-01-06 00:00:00+00 - 2023-02-03 00:00:00+00 I would like to get a list of days where the first element is 2023-01-06 and the last is 2023-02-02 as effectively 2023-02-03 hasn't started. That series still gives me an output that includes 2023-02-03, which is not what I want:
generate_series('2023-01-06 00:00:00+00'::date, '2023-02-03 00:00:00+00'::date, '1 day')
Is that possible to achieve in postgres?
you could check if ot os midnight and then subtract 1 Minute or 1 second from the end date
SELECt *
FROM generate_series('2023-01-06 00:00:00+00'::date,
(CASE WHEN to_char('2023-02-03 00:00:00+00'::date, 'HH24:MI:SS') = '00:00:00' THEN
'2023-02-03 00:00:00+00'::date - interval '1 Minute'
ELSE '2023-02-03 00:00:00+00'::date END) , '1 day')
generate_series
2023-01-06 00:00:00
2023-01-07 00:00:00
2023-01-08 00:00:00
2023-01-09 00:00:00
2023-01-10 00:00:00
2023-01-11 00:00:00
2023-01-12 00:00:00
2023-01-13 00:00:00
2023-01-14 00:00:00
2023-01-15 00:00:00
2023-01-16 00:00:00
2023-01-17 00:00:00
2023-01-18 00:00:00
2023-01-19 00:00:00
2023-01-20 00:00:00
2023-01-21 00:00:00
2023-01-22 00:00:00
2023-01-23 00:00:00
2023-01-24 00:00:00
2023-01-25 00:00:00
2023-01-26 00:00:00
2023-01-27 00:00:00
2023-01-28 00:00:00
2023-01-29 00:00:00
2023-01-30 00:00:00
2023-01-31 00:00:00
2023-02-01 00:00:00
2023-02-02 00:00:00
SELECT 28
fiddle

get time series in 8 hours of interval

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

Multiplying a timestamp data for several times in BigQuery [duplicate]

This question already has answers here:
Is there a SQL function to expand table?
(4 answers)
Closed 3 years ago.
I have a time-series starting from 2017-01-01 00:00:00 to the end of 2017-12-31 23:00:00 for 1-hour interval. I need to duplicate this 1-year timestamp for 2400 times in the same column. I need help about this one..
Row Date_time
1 2017-01-01 00:00:00 UTC
2 2017-01-01 01:00:00 UTC
3 2017-01-01 02:00:00 UTC
4 2017-01-01 03:00:00 UTC
5 2017-01-01 04:00:00 UTC
6 2017-01-01 05:00:00 UTC
7 2017-01-01 06:00:00 UTC
8 2017-01-01 07:00:00 UTC
...........................
...........................
You would do this in BigQuery by generating a timestamp array and then unnesting:
select ts
from unnest(generate_timestamp_array('2017-01-01 00:00:00', '2017-12-31 23:00:00', interval 1 hour)) ts
You can then get multiple rows with a similar construct:
select ts
from unnest(generate_timestamp_array('2017-01-01 00:00:00', '2017-12-31 23:00:00', interval 1 hour)
) ts cross join
unnest(generate_series(1, 2400)) n

SQL - move date to within 48 hr window

I have a bunch of historic timestamp dates. Basically, I need to simulate a new date such that the historic dates are moved to within a 48 hour window of the current date.
This is an extract of the date column:
2019-05-07 17:46:57.733 UTC
2019-05-15 13:03:25.247 UTC
2019-05-07 13:27:49.453 UTC
2019-05-11 04:24:02.293 UTC
2019-04-18 08:00:54.660 UTC
2019-04-25 05:34:36.777 UTC
2019-05-14 16:48:07.863 UTC
Assuming the current date is 2019-10-03 15:00:00. The expected range of dates should be between 2019-10-03 15:00:00 and 2019-10-01 15:00:00
The expected results should be the following.
2019-10-02 17:46:57.733 UTC
2019-10-03 13:03:25.247 UTC
2019-10-03 13:27:49.453 UTC
2019-10-03 04:24:02.293 UTC
2019-10-02 08:00:54.660 UTC
2019-10-02 05:34:36.777 UTC
2019-10-01 16:48:07.863 UTC
Why not just construct two days of random timestamps?
select timestamp_add(current_timestamp, interval cast(rand() * (60 * 60 * 24 * 2) as int64) second)
from t
It feels like you are looking for a random date function.
CREATE TEMP FUNCTION random_date()
RETURNS DATE
AS ( DATE_SUB(CURRENT_DATE(), INTERVAL CAST(FLOOR(RAND() * 29 / 10) AS INT64) DAY));
with data as (
select "2019-05-07 17:46:57.733 UTC" as date_time UNION ALL
select "2019-05-15 13:03:25.247 UTC" UNION ALL
select "2019-05-07 13:27:49.453 UTC" UNION ALL
select "2019-05-11 04:24:02.293 UTC" UNION ALL
select "2019-04-18 08:00:54.660 UTC" UNION ALL
select "2019-04-25 05:34:36.777 UTC" UNION ALL
select "2019-05-14 16:48:07.863 UTC" )
SELECT
CONCAT(FORMAT_DATE("%Y-%m-%d", random_date()), " ", SUBSTR(date_time, 12))
FROM data;
Output:
+-----------------------------+
| f0_ |
+-----------------------------+
| 2019-10-01 17:46:57.733 UTC |
| 2019-10-01 13:03:25.247 UTC |
| 2019-10-02 13:27:49.453 UTC |
| 2019-10-03 04:24:02.293 UTC |
| 2019-10-03 08:00:54.660 UTC |
| 2019-10-03 05:34:36.777 UTC |
| 2019-10-02 16:48:07.863 UTC |
+-----------------------------+

Select Every Nth Record From SQL

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))