Having trouble with SQL Interval - sql

I need to get the sum of data from a particular date going back one year. But I am having difficult with the Interval
Select 0 - sum(l.icdetailquantity )
from inventoryline l
where l.icmasterid = 'WAD185967E' and l.icdetailtranstype = 5
and l.icdetaildate <= '01-Jan-2019' and l.icdetaildate > Now() - interval '1 year'
This works as expected but I need to passs a date not use Now()
Select 0 - sum(l.icdetailquantity )
from inventoryline l
where l.icmasterid = 'WAD185967E' and l.icdetailtranstype = 5
and l.icdetaildate <= '01-Jan-2019' and l.icdetaildate > '01-Jan-2019' - interval '1 year'
the second SQl give an error
ERROR: invalid input syntax for type interval: "01-Jan-2019"
LINE 4: ...detaildate <= '01-Jan-2019' and l.icdetaildate > '01-Jan-20...

You're trying to subtract a year from a string. This does not work, you have to first cast youru string to a date object using:
to_date(date_string,format)
Example that will work for you:
to_date('01-Jan-2019', 'DD-Mon-YYYY') - interval '1 year'
http://www.postgresqltutorial.com/postgresql-to_date/

Have you tried with
Select 0 - sum(l.icdetailquantity )
from inventoryline l
where l.icmasterid = 'WAD185967E'
and l.icdetailtranstype = 5
and l.icdetaildate <= date '2019-01-01'
and l.icdetaildate > date '2019-01-01' - interval '1 year'
From the documentation I'm looking at (https://www.postgresql.org/docs/current/functions-datetime.html) the "date" keyword seems to be necessary.

Related

SQL Conditional Select Help (Amazon Redshift SQL)

I currently have 2 queries that I am trying to merge into 1. Basically I want to pull previous days sales if it's Tuesday - Friday and the previous 3 days sales if it's Monday. My queries are below - is there a way to do a conditional select for those date based on the day of the week?
Monday's version
SELECT *
FROM A
WHEN DATE_TRUNC('day', timestamp) IN (CURRENT_DATE - 1, CURRENT_DATE - 2, CURRENT_DATE - 3)
AND DATE_PART(weekday, current_date) = 1
Tuesday - Friday version
SELECT *
FROM A
WHEN DATE_TRUNC('day', timestamp) = CURRENT_DATE - 1
AND DATE_PART(weekday, current_date) = 1
one solution is :
SELECT *
FROM A
WHERE DATE_TRUNC('date', timestamp) <= DATE_TRUNC('date', CURRENT_DATE)
AND DATE_TRUNC('date', timestamp) >= case when DATE_TRUNC('day of week',CURRENT_DATE) = 'Monday' then DATE_TRUNC('date', CURRENT_DATE - 3) else DATE_TRUNC('date',CURRENT_DATE) end
You can use `OR:
SELECT *
FROM A
WHERE (DATE_TRUNC('day', timestamp) IN (CURRENT_DATE - 1, CURRENT_DATE - 2, CURRENT_DATE - 3) AND
DATE_PART(weekday, current_date) = 1
) OR
(DATE_TRUNC('day', timestamp) = CURRENT_DATE - 1 AND
DATE_PART(weekday, current_date) <> 1
)
This can be slightly simplified to:
WHERE timestamp > CURRENT_DATE -
(CASE WHEN DATE_PART(weekday, current_date) = 1 INTERVAL '3 DAY'
ELSE INTERVAL '1 DAY'
END)
Note: This assumes that there are no future timestamps.

sql search in a date column from current date to current date+7 not working

This query works and brings up proper results:
SELECT *
FROM WJ07LG4.appointments
WHERE start >= current_date and start <= '2020-12-17';
This query brings up no results even though there are dates within a week of now:
SELECT *
FROM WJ07LG4.appointments
WHERE start >= current_date and start <= current_date + 7;
Can someone please tell me what is wrong?
SELECT *
FROM WJ07LG4.appointments
WHERE start >= current_date and start <= date_add(current_date, INTERVAL 7 DAY);
is what solved it. subodh led me in the right direction
Try
SELECT *
FROM WJ07LG4.appointments
WHERE start >= current_date and start <= DATEADD(dd, 7, current_date );
More info DATEADD
You may verify if the data type for both the 'current_date' and 'start' column is either of Date, DateTime,  DateTime2, or SmallDatetime.
If you just select the results, you can se what is happening:
current_date current_date + 7
2020-11-29 20201136
Hmmm, Nov. 36? What date is that? Oh! What is happening is that the database is converting the "date" to an integer and just adding 7.
The simplest solution is to use interval arithmetic:
current_date + interval 7 day
In your code, that looks like:
WHERE start >= current_date AND
start <= current_date + INTERVAL 7 DAY

invalid value "(202" for "yyyy" error in generate_series() with dates in PostgreSQL

I'm trying to generate dynamic dates based on the current date. I want to use generate_series() to populate dates between start and end dates (interval = 1 day).
If current date is before 10/1, start date is 10/1 in previous year
If current date is after 10/1, start date is 10/1 in the current year
end date is 9/30 in year 4. For example,
current date = 5/22/2019 -> start date = 10/1/2018, end date = 9/30/2021
current date = 11/1/2019 -> start date = 10/1/2019, end date = 9/30/2022
select generate_series(
to_date(cast(start_date as text), 'yyyy-mm-dd'),
to_date(concat(extract(year from to_date(cast(start_date as text), 'yyyy-mm-dd')+3),'-','09','-', 30), 'yyyy-mm-dd'),
'1 day'
)
from (
select case
when extract(month from current_date) <= 10 then concat(extract(year from current_date) -1,'-',10,'-', '01')
when extract(month from current_date) > 10 then concat(extract(year from current_date),'-',10,'-', '01')
end) as start_date
ERROR: invalid value "(202" for "yyyy"
DETAIL: Value must be an integer.
SQL state: 22007
It's complaining about year isn't integer. Which parts do I need to modify to run this query?
select case
when date_trunc('month', current_date) ::date < make_date(extract(year from current_date) ::int, 10, 1) then
generate_series(make_date((extract(year from current_date) - 1) ::int, 10, 1)
,make_date((extract(year from current_date) + 2) ::int, 10, 1) - 1
,'1 day') ::date
else
generate_series(make_date(extract(year from current_date) ::int, 10, 1)
,make_date((extract(year from current_date) + 3) ::int, 10, 1) - 1
,'1 day') ::date
end as dt;
Here In place of current_date you can use as below: current_date => '11/1/2019'::date or '05/22/2019'::date

How to filter out intraweek data in PostgreSQL?

I have a query that I'm leveraging for a dashboard visualization. The only issue with the query is that it includes data for the current week - which is problematic because the week is not fully baked. Ideally, I would like to add a filter that pulls in data w/in the last 52 weeks but excludes data with a date that is greater than the most recent reporting week (Sun - Sat).
Here is what I currently have:
SELECT (DATE_TRUNC('week',cj.created_at:: timestamptz) + '5 days'::
interval)::date
,CASE WHEN o.vertical IS NULL OR o.vertical NOT IN
('Auto','Franchise') THEN 'SMB'
ELSE o.vertical END as vertical
,COUNT(DISTINCT cj.native_candidate_job_id) as applicant_traffic
FROM dim_candidate_jobs cj
JOIN dim_organizations o ON cj.native_organization_id =
o.native_organization_id
WHERE o.demo = false
AND NOT(o.name ~~* any (array['%test%','%api%']))
AND cj.created_at:: date > current_date - interval '52 weeks'
AND cj.created_at:: date < (DATE_TRUNC('week',current_date::
timestamptz) + '1 day':: interval)::date
AND o.current = 'Y'
AND cj.current = 'Y'
GROUP BY 1,2;;
and cj.created_at >= date_trunc('week', current_date) - interval '52 weeks'
and cj.created_at < date_trunc('week', current_date)

Interchange String with Date in query

I am running the below query. I would like to make this query more dynamic, however, so I would like to use the 2nd query instead.
SELECT X
FROM Y
WHERE Z
and file_created_date = '12/18/2016'
SELECT X
FROM Y
WHERE Z
and file_created_date = SELECT date(GETDATE()-2)
The 2nd query returns an error around the Select of
SELECT date(GETDATE()-2)
GETDATE() isn't available in postgres. You can however use current_date i.e.
... and file_created_date = current_date - 2
Postgres supports interval offsets for dates:
where Z and
file_created_date = current_date - interval '2 day'
If file_created_date also has a time component, then you should take that into account:
where Z and
file_created_date > current_date - interval '3 day' and
file_created_date <= current_date - interval '2 day'