I have a view that lists certain events taking place the next day
SELECT column1, column2...
FROM table1
WHERE date = CAST(CURRENT_DATE + INTERVAL '1 DAY' AS DATE)
Nevertheless, I have a table of 'forbidden dates': I cannot use this specific set of dates, so, in case the next day is in that forbidden list, it must jump to the next one. Sort of like this:
SELECT column1, column2...
FROM table1
WHERE
CASE
WHEN CAST(CURRENT_DATE + INTERVAL '1 DAY' AS DATE) IN (SELECT DISTINCT date FROM forbidden_date)
THEN CAST(CURRENT_DATE + INTERVAL '2 DAY' AS DATE)
ELSE
CAST(CURRENT_DATE + INTERVAL '1 DAY' AS DATE)
END = date
The problem is that "what if the second next day is also in the forbidden list? and so on and on?"
I actually could control all this from a script, but I'm just really curious if I could make it through with just a query
Use generate_series to return all dates from the next 365 days and chose the first one that isn't a forbidden date:
SELECT column1, column2...
FROM table1
WHERE date = (
select min(d)
from generate_series(current_date + 1, current_date + 365, '1 day') as dates(d)
where d not in (select date from forbidden_dates))
Related
I have a table with two columns, dates and number of searches in each date. What I want to do is group by the dates, and find the sum of number of searches for each date.
The trick is that for each group, I also want to include the number of searches for the date exactly the following week, and the number of searches for the date exactly the previous week.
So If I have
Date
Searches
2/3/2023
2
2/10/2023
4
2/17/2023
1
2/24/2023
5
I want the output for the 2/10/2023 and 2/17/2023 groups to be
Date
Sum
2/10/2023
7
2/17/2023
10
How can I write a query for this?
You can use a correlated query for this:
select date, (
select sum(searches)
from t as x
where x.date between t.date - interval '7 day' and t.date + interval '7 day'
) as sum_win
from t
Replace interval 'x day' with the appropriate date add function for your RDBMS.
If your RDBMS supports interval in window functions then a much better solution would be:
select date, sum(searches) over (
order by date
range between interval '7 day' preceding and interval '7 day' following
) as sum_win
from t
Assuming weekly rows
CREATE TABLE Table1
([Dates] date, [Searches] int)
;
INSERT INTO Table1
([Dates], [Searches])
VALUES
('2023-02-03 00:00:00', 2),
('2023-02-10 00:00:00', 4),
('2023-02-17 00:00:00', 1),
('2023-02-24 00:00:00', 5)
;
;with cte as (
select dates
, searches
+ lead(searches) over(order by dates)
+ lag(searches) over(order by dates) as sum_searches
from table1)
select * from cte
where sum_searches is not null;
dates
sum_searches
2023-02-10
7
2023-02-17
10
fiddle
I would like to provide the user to option to select a range of date in current month and results should be comparison of same date range for current & previous month.
Eg. selected date 1-12-2022 to 15-12-2022
Result:
Count X 1-11-2022 to 15-11-2022
Count X 1-12-200 to 15-12-2022
Can this be achieved through date_part function?
Suppose you have a column with type date named ts in your table:
SELECT
count(*) FILTER ( WHERE ts BETWEEN cast(:lower AS DATE) - INTERVAL '1 month' AND cast(:upper AS DATE) - INTERVAL '1 month') previous,
count(*) FILTER ( WHERE ts BETWEEN cast(:lower AS DATE) AND cast(:upper AS DATE) ) selected,
count(*) FILTER ( WHERE ts BETWEEN cast(:lower AS DATE) + INTERVAL '1 month' AND cast(:upper AS DATE) + INTERVAL '1 month' ) next
FROM your_table;
You just need to provide the lower and upper bound values as dates (e.g. '01-12-2022'.
This will give you 3 columns -- previous, selected and next -- with the corresponding row-counts.
BTW: the upper bound is exclusive.
I have a query that can create a table with dates like below:
with digit as (
select 0 as d union all
select 1 union all select 2 union all select 3 union all
select 4 union all select 5 union all select 6 union all
select 7 union all select 8 union all select 9
),
seq as (
select a.d + (10 * b.d) + (100 * c.d) + (1000 * d.d) as num
from digit a
cross join
digit b
cross join
digit c
cross join
digit d
order by 1
)
select (last_day(sysdate)::date - seq.num)::date as "Date"
from seq;
How could this be changed to generate only dates
Thanks
demo:db<>fiddle
WITH dates AS (
SELECT
date_trunc('month', CURRENT_DATE) AS first_day_of_month,
date_trunc('month', CURRENT_DATE) + interval '1 month -1 day' AS last_day_of_month
)
SELECT
generate_series(first_day_of_month, last_day_of_month, interval '1 day')::date
FROM dates
date_trunc() truncates a type date (or timestamp) to a certain date part. date_trunc('month', ...) removes all parts but year and month. All other parts are set to their lowest possible values. So, the day part is set to 1. That's why you get the first day of month with this.
adding a month returns the first of the next month, subtracting a day from this results in the last day of the current month.
Finally you can generate a date series with start and end date using the generate_series() function
Edit: Redshift does not support generate_series() with type date and timestamp but with integer. So, we need to create an integer series instead and adding the results to the first of the month:
db<>fiddle
WITH dates AS (
SELECT
date_trunc('month', CURRENT_DATE) AS first_day_of_month,
date_trunc('month', CURRENT_DATE) + interval '1 month -1 day' AS last_day_of_month
)
SELECT
first_day_of_month::date + gs
FROM
dates,
generate_series(
date_part('day', first_day_of_month)::int - 1,
date_part('day', last_day_of_month)::int - 1
) as gs
This answers the original version of the question.
You would use generate_series():
select gs.dte
from generate_series(date_trunc('month', now()::date),
date_trunc('month', now()::date) + interval '1 month' - interval '1 day',
interval '1 day'
) gs(dte);
Here is a db<>fiddle.
There's a table dates_calendar:
id | date
-------------------------
13 | 2016-10-23 00:00:00
14 | 2016-10-24 00:00:00
I need to update this table and insert dates until the next month counting from the last date in the table. E.g. last date is 2016-10-24 00:00:00 - I need to insert dates till 2016-10-31. After that (the last date now is 2016-10-31) next statement call should insert dates till 2016-11-30 and so on.
Example of my SQL code, but it inserts 30 days all the time.
INSERT INTO dates_calendar (date)
VALUES (
generate_series(
(SELECT date FROM dates_calendar ORDER BY date DESC LIMIT 1) + interval '1 day',
(SELECT date FROM dates_calendar ORDER BY date DESC LIMIT 1) + interval '1 month',
'1 day'
)
);
I'm using PostgreSQL. As well would be fine to get rid of a duplicated SELECT statement of the last date.
insert into dates_calendar (date)
select dates::date
from (
select max(date)::date+ 1 next_day, '1day'::interval one_day, '1month'::interval one_month
from dates_calendar
) s,
generate_series(
next_day,
date_trunc('month', next_day)+ one_month- one_day,
one_day) dates;
To calculate the first and last date you need to insert you can use this query:
select max(date) + interval '1' day as first_day,
date_trunc('month', max(date) + interval '1' month) - interval '1' day as last_day
from dates_calendar
The expression date_trunc('month', max(date) + interval '1' month) calculates the start date of the next month. Subtracting one day from that will give you the last day of that month.
This can then be used to generate the list of dates:
with from_to (first_day, last_day) as (
select max(date) + interval '1' day,
date_trunc('month', max(date) + interval '1' month) - interval '1' day
from dates_calendar
)
select dt
from generate_series( (select first_day from from_to), (select last_day from from_to), interval '1' day) as t(dt);
And finally this can be used to insert the generated rows into the table:
with from_to (first_day, last_day) as (
select max(date) + interval '1' day,
date_trunc('month', max(date) + interval '1' month) - interval '1' day
from dates_calendar
)
insert into dates_calendar (date)
select dt
from generate_series( (select first_day from from_to), (select last_day from from_to), interval '1' day) as t(dt);
with max_date (d) as (select max(date)::date from dates_calendar)
insert into dates_calendar (date)
select d
from generate_series (
(select d from max_date) + 1,
(select date_trunc('month', d + interval '1 month')::date - 1 from max_date),
'1 day'
) g(d)
I have a table of driver licenses per person and know when a person acquired the drivers license. The validity of the drivers license can either be X days from the day you acquired it or a specific date.
acquired relative specific_date valid_type expiration_date
----------------------------------------------------------------------------------
2015-02-05 500 null days
2015-02-05 null 2016-03-05 date
2015-02-05 200 null days
2015-02-05 null 2016-02-22 date
My query right now would be:
SELECT acquired,
relative_date,
specific_date,
valid_type
FROM person_drivers_license
WHERE (valid_type = 'days'
AND (EXTRACT(epoch
FROM acquired) - EXTRACT(epoch
FROM now()))/86400 + relative_date > 0)
OR (valid_type = 'DATE'
AND specific_date >= now()));
I am trying to add an expiration_date column with the select statement above. If it is a specific date, just take the date and put it in expiration_date and if it is a relative date, calculate the expiration date with the help of the acquired date. Is this possible in PSQL?
First - there is a simpler way to do date math in postgres. You can use something like:
acquired + relative_date * interval '1 day' >= current_date
or
acquired + relative_date >= current_date
-- any integer can be treated as interval in days for date mathematics is SQL
For the question - try one of this:
CASE WHEN valid_type = 'days'
THEN acquired + relative_date * interval '1 day'
WHEN valid_type = 'date'
THEN specific_date
--ELSE ??? you may specify something here
END
or
COALESCE(specific_date, acquired + relative_date * interval '1 day')
The query may look like:
SELECT acquired,
relative_date,
specific_date,
valid_type,
COALESCE(specific_date, acquired + relative_date * interval '1 day') as valid_date
FROM person_drivers_license
WHERE COALESCE(specific_date, acquired + relative_date * interval '1 day') >= current_date
Try this:
SELECT acquired,
relative_date,
specific_date,
valid_type
CASE valid_type
WHEN 'days' THEN acquired + relative_date
WHEN 'date' THEN specific_date
ELSE NULL
END AS expiration_date
FROM person_drivers_license;
You don't need pl/sql
SELECT acquired,
relative_date,
specific_date,
valid_type,
CASE WHEN specific date = "date" -- starts here
THEN specific date
ELSE acquired + cast('1 months' as interval)END
AS expiration_date -- end here
FROM person_drivers_license
WHERE (valid_type = 'days'
AND (EXTRACT(epoch
FROM acquired) - EXTRACT(epoch
FROM now()))/86400 + relative_date > 0)
OR (valid_type = 'DATE'
AND specific_date >= now()));