How to convert/re-write the below SQL to PostgreSQL? I'm not sure how to convert the connect by level and listagg in postgreSQL.
select listagg(dt,' ') within group (order by lvl), NBR
from
(
select level lvl,
case when level=1 then TO_CHAR(a.dt2,'MM/DD/YYYY HH24:MI:SS')
else
TO_CHAR(a.dt2+(1/1440*30*(level-1)),'MM/DD/YYYY HH24:MI:SS')
end
dt,10 NBR from
(select to_date('08/11/2021 18:30:00','MM/DD/YYYY HH24:MI:SS') dt1,to_date('08/11/2021 01:30:00','MM/DD/YYYY HH24:MI:SS') dt2 from dual) a
start with level=0
connect by level<=1+(to_date('08/11/2021 18:30:00','MM/DD/YYYY HH24:MI:SS')-to_date('08/11/2021 13:30:00','MM/DD/YYYY HH24:MI:SS'))*24*2)
GROUP BY NBR;
Output:
08/11/2021 01:30:00
08/11/2021 02:00:00
08/11/2021 02:30:00
08/11/2021 03:00:00
08/11/2021 03:30:00
08/11/2021 04:00:00
08/11/2021 04:30:00
08/11/2021 05:00:00
08/11/2021 05:30:00
08/11/2021 06:00:00
08/11/2021 06:30:00
Not sure what the group by nbr is supposed to achieve - as far as I can tell this serves no purpose.
The convoluted connect by level in Oracle can be replaced with a simple generate_series() in Postgres.
So the following will generate 11 timestamp values from 2021-08-11 01:30:00 to 2021-08-11 06:30:00:
select g.dt
from generate_series(timestamp '2021-08-11 01:30:00',
timestamp '2021-08-11 06:30:00',
interval '30 minute') as g(dt)
This can then be aggregated back into a string using string_agg()
select string_agg(to_char(dt, 'dd/mm/yyyy hh24:mi:ss'), ' '), 10 as nbr
from generate_series(timestamp '2021-08-11 01:30:00',
timestamp '2021-08-11 06:30:00',
interval '30 minute') as g(dt)
If you need the number of rows generated, you can use the with ordinality clause to get that:
select string_agg(to_char(dt, 'dd/mm/yyyy hh24:mi:ss'), ' '), max(idx) as nbr
from generate_series(timestamp '2021-08-11 01:30:00',
timestamp '2021-08-11 06:30:00',
interval '30 minute') with ordinality as g(dt,idx)
Related
I'm working on the Accrual Reversal query in PostgreSQL. The system running doesn't have the reversal flag. So I need to consider all the end of the day of previous month accrued invoices as the reversal amount. And need to union them all with the main query. I can do it for last month but invoice date are dynamic, user may give 2 years as invoice period. For those 2 years, all the previous month data should be considered as accrued reversal. Here is the query
select invoicename, * from accountpay where invoice_date between '2020-01-01' and '2021-12-31'
union all
select concat('Accured Reversal', invoicename) as reference, * from accountpay where accrual = true and invoice_date::date = (select concat(date_part('year',((('2021-12-30'::date) - interval '1 month'))), '-', date_part('month',((('2021-12-30'::date) - interval '1 month'))), '-01')::date + interval '1 month' - interval '1 day')
Please help me to do this.
Thanks in Advance
SELECT (
Date_trunc('MONTH',a) + interval '1 month -1 day ')
as last_day_of_month
FROM generate_series(
'2020-01-01 00:00'::timestamp
- interval '12 months',
'2022-01-01 00:00',
'1 month') as dt(a);
get last_day_of_month from '2020-01-01 00:00' till '2022-01-01 00:00'
Then your sql would be
invoice_date in
(SELECT (Date_trunc('MONTH',a) + interval '1 month -1 day ')
as last_day_of_month
FROM generate_series(
'2020-01-01 00:00'::timestamp
- interval '12 months',
'2021-01-01 00:00',
'1 month') as dt(a))
This will get the last day of last 12 months so the number of months will be place holder (dynamic) and all the last day of the months will be in IN clause.
SQL re-written:
WITH date_cte AS
(
SELECT Date_trunc('MONTH',dt)+ interval '1 month -1 day ' last_day_of_month
FROM generate_series('2021-11-30 00:00:00'::timestamp - interval '12 months','2021-11-30 00:00:00','1 month') t(dt))
select invoicename, * from accountpay where invoice_date between '2020-01-01' and '2021-12-31'
union all
select concat('Accured Reversal', invoicename) as reference, * from accountpay where accrual = true and invoice_date::date in (select * from date_cte);
Basically , the last dates are generated this way for 12 months:
WITH date_cte AS
(
SELECT Date_trunc('MONTH',dt)+ interval '1 month -1 day ' last_day_of_month
FROM generate_series('2021-11-30 00:00:00'::timestamp - interval '12 months','2021-11-30 00:00:00','1 month') t(dt))
SELECT *
FROM date_cte;
last_day_of_month
---------------------
2020-11-30 00:00:00
2020-12-31 00:00:00
2021-01-31 00:00:00
2021-02-28 00:00:00
2021-03-31 00:00:00
2021-04-30 00:00:00
2021-05-31 00:00:00
2021-06-30 00:00:00
2021-07-31 00:00:00
2021-08-31 00:00:00
2021-09-30 00:00:00
2021-10-31 00:00:00
2021-11-30 00:00:00
You can replace 12 months by any number of months or you can make it year too like:
...generate_series('2021-11-30 00:00:00'::timestamp - interval '1 year','2021-11-30 00:00:00','1 month')
select CAST(current_date as timestamp)
+ to_char(timestamp '2019-07-08 09:00:00','YYYY-MM-DD HH24:MI:SS')::TIMESTAMP;
Current output: 2019-08-02 00:00:002019-07-08 09:00:00
You can use the DATEADD() function or the INTERVAL syntax to add specific units of time to another date/timestamp.
SELECT DATEADD(hour, 9, current_date);
-- date_add
-- ---------------------
-- 2019-08-05 09:00:00
SELECT current_date + INTERVAL '9 hours';
-- ?column?
-- ---------------------
-- 2019-08-05 09:00:00
I also have the question how do i get code block to work on stack overflow but that's a side issue.
I have this quasi-code that works:
select
*
from
unnest('{2018-6-1,2018-7-1,2018-8-1,2018-9-1}'::date[],
'{2018-6-30,2018-7-31,2018-8-31,2018-9-30}'::date[]
) zdate(start_date, end_date)
left join lateral pipe_f(zdate...
But now I want it to work from 6/1/2018 until now(). What's the best way to do this.
Oh, postgresql 10. yay!!
Your query gives a list of first and last days of months between "2018-06-01" and now. So I am assuming that you want to this in a more dynamic way:
demo: db<>fiddle
SELECT
start_date,
(start_date + interval '1 month -1 day')::date as end_date
FROM (
SELECT generate_series('2018-6-1', now(), interval '1 month')::date as start_date
)s
Result:
start_date end_date
2018-06-01 2018-06-30
2018-07-01 2018-07-31
2018-08-01 2018-08-31
2018-09-01 2018-09-30
2018-10-01 2018-10-31
generate_series(timestamp, timestamp, interval) generates a list of timestamps. Starting with "2018-06-01" until now() with the 1 month interval gives this:
start_date
2018-06-01 00:00:00+01
2018-07-01 00:00:00+01
2018-08-01 00:00:00+01
2018-09-01 00:00:00+01
2018-10-01 00:00:00+01
These timestamps are converted into dates with ::date cast.
Then I add 1 month to get the next month. But as we are interested in the last day of the previous month I subtract one day again (+ interval '1 month -1 day')
Another option that's more ANSI-compliant is to use a recursive CTE:
WITH RECURSIVE
dates(d) AS
(
SELECT '2018-06-01'::TIMESTAMP
UNION ALL
SELECT d + INTERVAL '1 month'
FROM dates
WHERE d + INTERVAL '1 month' <= '2018-10-01'
)
SELECT
d AS start_date,
-- add 1 month, then subtract 1 day, to get end of current month
(d + interval '1 month') - interval '1 day' AS end_date
FROM dates
There is one table:
ID DATE
1 2017-09-16 20:12:48
2 2017-09-16 20:38:54
3 2017-09-16 23:58:01
4 2017-09-17 00:24:48
5 2017-09-17 00:26:42
..
The result I need is the last 7-days of data with hourly aggregated count of rows:
COUNT DATE
2 2017-09-16 21:00:00
0 2017-09-16 22:00:00
0 2017-09-16 23:00:00
1 2017-09-17 00:00:00
2 2017-09-17 01:00:00
..
I tried different stuff with EXTRACT, DISTINCT and also used the generate_series function (most stuff from similar stackoverflow questions)
This try was the best one currently:
SELECT
date_trunc('hour', demotime) as date,
COUNT(demotime) as count
FROM demo
GROUP BY date
How to generate hourly series for 7 days and fill-in the count of rows?
SQL DEMO
SELECT dd, count("demotime")
FROM generate_series
( current_date - interval '7 days'
, current_date
, '1 hour'::interval) dd
LEFT JOIN Table1
ON dd = date_trunc('hour', demotime)
GROUP BY dd;
To work from now and now - 7 days:
SELECT dd, count("demotime")
FROM generate_series
( date_trunc('hour', NOW()) - interval '7 days'
, date_trunc('hour', NOW())
, '1 hour'::interval) dd
LEFT JOIN Table1
ON dd = date_trunc('hour', demotime)
GROUP BY dd;
Postgresql extraction between two DateTime but ignore only seconds (seconds for two datetime = 00)
06.09.2014 18:54:35 - 06.09.2014 18:54:35
DD.MM.YYYY HH24:MI - DD.MM.YYYY HH24:MI
It sounds like you're looking for the date_trunc() function.
with test_data as (
select timestamp '2015-01-01 08:00:13' as ts union all
select timestamp '2015-01-01 08:13:27' union all
select timestamp '2015-01-01 09:00:27' union all
select timestamp '2015-01-01 09:01:42'
)
select *, date_trunc('minute', ts)
from test_data
where date_trunc('minute', ts) between '2015-01-01 08:00' and '2015-01-01 09:00';
ts date_trunc
--
2015-01-01 08:00:13 2015-01-01 08:00:00
2015-01-01 08:13:27 2015-01-01 08:13:00
2015-01-01 09:00:27 2015-01-01 09:00:00
If you need this kind of query to use an index, you'll need to create an index on the expression date_trunc('minute', your_column_name).