How to select data in 2 month(from now) use postgresql? - postgresql

I'm new to sql and database work . Now I want select data in 2 month from now . the key is xxdate lookslike 2019-4-11
like:
select * from table where date > now() - 2 month
but I don't know the correct way to express it. can someone help?

You can use this query:
SELECT * FROM table WHERE date > (current_date - interval '2 month')::date;

Related

Netezza - find the first date of the prior quarter

I'm trying to get the first day of the previous quarter from today's date however I can't find logic for Netezza SQL.
For SQL Server I could use the following:
select dateadd(quarter, datediff(quarter, 0, getdate()) - 1, 0)
There doesn't appear to be an equivalent of datediff in Netezza, any suggestions would be greatly appreciated
=> select now(), (date_trunc('quarter', now()) - interval ('3 months'))::date as result;
NOW | RESULT
---------------------+------------
2022-09-02 13:09:05 | 2022-04-01
(1 row)
Based on a similar answer here I was able to adapt my code to the following:
WHERE
TABLE_A.DATE_FIELD BETWEEN (SELECT TO_DATE(TO_CHAR(TO_DATE(TO_CHAR(CURRENT_DATE, 'YYYYQ'),'YYYYQ')-1,'YYYYQ'),'YYYYQ')) AND (SELECT TO_DATE(TO_CHAR(CURRENT_DATE,'YYYYQ'),'YYYYQ'))

PSQL generate_series inserting series of dates?

I have the following column in a table:
account_no (text)
total_equity (float64)
insertion_date (timestamptz)
How can I insert dummy datas into the table with a series of date like what below script. What I currently have, which obviously wrong:
select
'123123' as account_no,
random() * 100000 + 1000000000 as total_equity,
insertion_date::timestamptz,
from
generate_series('2022-03-03'::timestamptz, '2022-05-09'::timestamptz, interval '1 day') as insertion_date;
What I'm trying to achieve is that I inserted data from 22-03-03 to 22-05-09 with a static "account_no" and randomized total_equity.
Thanks in advance!

How to parameterize a query using INTERVAL

The below query is syntactically correct, but it's not returning anything. I'm trying to find all data older than ? months. I've been testing with 1 month, or even 1 day, but I don't get any data. Is there anything wrong with this query?
SELECT * FROM detail t
WHERE t.job_start_time < now() - (? * INTERVAL '1 MONTHS')
I prefer using make_interval():
SELECT *
FROM detail t
WHERE t.job_start_time < now() - make_interval(months => ?)

Adding one year to a date field in postgresql

I have a table in postgresql with a field_date using the syntax 'YYYY-MM-DD', I want to add a year to the field with the the sentence:
UPDATE table SET date_field = DATEADD(YEAR, 1, date_field);
but postgres return:
ERROR: column "year" does not exist
I can't see what's wrong with the sentence
Try this:
UPDATE table SET date_field = date_field + interval '1 year'
It appears that you were trying to use SQL Server's DATEADD() function, which does not exist in Postgres.
select CURRENT_DATE, CURRENT_DATE + interval '1 year'
In your case you trying to do it in Transact SQL not in Postgres.
Try to use:
DATEADD-> "DATE + interval '1 year'"

generate_series function in Amazon Redshift

I tried the below:
SELECT * FROM generate_series(2,4);
generate_series
-----------------
2
3
4
(3 rows)
SELECT * FROM generate_series(5,1,-2);
generate_series
-----------------
5
3
1
(3 rows)
But when I try,
select * from generate_series('2011-12-31'::timestamp, '2012-12-31'::timestamp, '1 day');
It generated error.
ERROR: function generate_series(timestamp without time zone, timestamp without time zone, "unknown") does not exist
HINT: No function matches the given name and argument types. You may need to add explicit type casts.
I use PostgreSQL 8.0.2 on Redshift 1.0.757.
Any idea why it happens?
UPDATE:
generate_series is working with Redshift now.
SELECT CURRENT_DATE::TIMESTAMP - (i * interval '1 day') as date_datetime
FROM generate_series(1,31) i
ORDER BY 1
This will generate last 30 days date
The version of generate_series() that supports dates and timestamps was added in Postgres 8.4.
As Redshift is based on Postgres 8.0, you need to use a different way:
select timestamp '2011-12-31 00:00:00' + (i * interval '1 day')
from generate_series(1, (date '2012-12-31' - date '2011-12-31')) i;
If you "only" need dates, this can be abbreviated to:
select date '2011-12-31' + i
from generate_series(1, (date '2012-12-31' - date '2011-12-31')) i;
generate_series is working with Redshift now.
SELECT CURRENT_DATE::TIMESTAMP - (i * interval '1 day') as date_datetime
FROM generate_series(1,31) i
ORDER BY 1
This will generate last 30 days date
I found a solution here for my problem of not being able to generate a time dimension table on Redshift using generate_series(). You can generate a temporary sequence by using the following SQL snippet.
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 (getdate()::date - seq.num)::date as "Date"
from seq;
The generate_series() function, it seems, is not supported completely on Redshift yet. If I run the SQL mentioned in the answer by DJo, it works, because the SQL runs only on the leader node. If I prepend insert into dim_time to the same SQL it doesn't work.
There is no generate_series() function in Redshift for Date Range but you can generate the series with below steps...
Step 1: Created a table genid and insert constant value as 1 for number of times you need to generate the series. If you need the series to be generated for 12 month you can insert 12 times. Better you can insert for more number of times like 100, so that you do not face any issue.
create table genid(id int)
------------ for number of months
insert into genid values(1)
Step 2: The table for which you need to generate the series.
create table pat(patid varchar(10),stdt timestamp, enddt timestamp);
insert into pat values('Pat01','2018-03-30 00:00:00.0','2018-04-30 00:00:00.0')
insert into pat values('Pat02','2018-02-28 00:00:00.0','2018-04-30 00:00:00.0')
insert into pat values('Pat03','2017-10-28 00:00:00.0','2018-04-30 00:00:00.0')
Step 3: This query will generate the series for you.
with cte as
(
select max(enddt) as maxdt
from pat
) ,
cte2 as(
select dateadd('month', -1 * row_number() over(order by 1), maxdt::date ) as gendt
from genid , cte
) select *
from pat, cte2
where gendt between stdt and enddt