How do get data from the beginning of five fiscal quarters ago - sql

I am tasked with getting data from beginning five fiscal quarters ago until present. This was not easy for an oracle novice like myself. Our fiscal year begins 1 November so this posed another dimension. Anyway, this code seems to work just fine. Just thought I would share and who knows, maybe you will find a mistake or better way.

Few observations.
ADD_MONTHS(sysdate - interval '2' year, -3) can be simplified to sysdate - interval '2-3' year to month.
TRUNC(date) gives a date output. So no need to use TO_DATE function on it.
I think ADD_MONTHS(sysdate - interval '2' year, +1) is wrong. It should be simply sysdate - interval '2' year. You can check by substituting
sysdate with a date in Decemeber, date'2013-12-04'.
Simplified query
select trunc(sysdate - interval '2-3' year to month,'Q') as beg_qtr,
trunc(sysdate - interval '2' year, 'Q') - 1 as end_qtr
from dual;

select (TO_DATE(TRUNC(ADD_MONTHS(sysdate - interval '2' year, -3),'Q'),
'DD-MM-YY')) AS BEG_QTR, TRUNC(ADD_MONTHS(sysdate - interval '2' year, +1), 'Q')
-1 AS END_QTR from DUAL

Related

Amazon Athena Date Functions [duplicate]

This question already has answers here:
Presto: Last day of the month prior
(2 answers)
Closed 4 months ago.
I understand Athena uses Presto, however the function last_day_of_month(x) in the documentation doesn't seem to work in AWS Athena.
Is there a function I can use to get the last day of the previous month based on the current date (30 september 2021), last day of previous year (31 december 2021) and last day of the half year (30 June 2022) etc?
I used the below script to do this, however it would be good to know if there's a function I can use or simpler way to run the dates.
SELECT date_trunc('month', current_date) - interval '1' day
SELECT date_trunc('year',(date_trunc('month', current_date) - interval '1' day)) - interval '1' day
SELECT date_add('month',6, date_trunc('year',(date_trunc('month', current_date) - interval '1' day)) - interval '1' day)
First, you need to upgrade your Workgroup to use the Athena engine version 3, which already supports last_day_of_month(x) function.
Athena is based on Presto/Trino, and each version of the Athena engine is based on a different version of the open-source project. You can control the version from the Workgroups menu and even let Athena upgrade the engine automatically for you.
Second, if you want a get the last day of the previous month, the easiest way is to create the first day of the following month and substruct one day from it.
SELECT date '2012-08-01' - interval '1' day
Therefore, if you want the last day of the previous month, and as suggested in the comment, using date_trunc:
SELECT date_trunc('month', current_date ) - interval '1' day
--- half year back
SELECT date_trunc('month', current_date - interval '6' month) - interval '1' day
--- one year back
SELECT date_trunc('month', current_date - interval '1' year) - interval '1' day

postgresql query to return the day of the week of the first day of the month two years from today

I used postgresql to solve the quesion, query to return the day of the week of the first day of the month two years from today. I was able to solve it with the query below, but I am not sure my query is correct, I just wanna make sure
select cast(date_trunc('month', current_date + interval '2 years') as date)
You are correctly computing the first day of the month two years later with:
date_trunc('month', current_date + interval '2 years')
If you want the corresponding day of the week, you can use extract();
extract(dow from date_trunc('month', current_date + interval '2 years'))
This gives you an integer value between 0 (Sunday) and 6 (Saturday)

SQL Function to select first day of current year and everyday after

I'm looking to get a quick and efficient code to get the first day of current year and everyday after that and their week numbers. So far I have something like this to start me off:
SELECT
to_char(TRUNC(SysDate,'YEAR'), 'WW')Week_No,
to_char(TRUNC(SysDate,'YEAR'), 'DD/MM/YY')First_Day
FROM dual;
Which gets me week number and first day of current year. I also have this to get me the last calendar day of current year:
SELECT ADD_MONTHS(trunc(sysdate,'YEAR'),12)-1 from dual;
I am being extremely silly or maybe it's end of the day brain mush but I'm trying to get the bit in the middle now. The point of this is to get a list of each calendar day and week number.
If anyone can guide me here I'd appreciate this.
Thanks
Are you looking for something like this:
SELECT
ST_DT + LEVEL - 1,
TO_CHAR(ST_DT + LEVEL - 1, 'IW') AS DY
FROM
(
SELECT
TRUNC(SYSDATE, 'YEAR') ST_DT,
TRUNC(SYSDATE, 'YEAR') + INTERVAL '12' MONTH - INTERVAL '1' DAY END_DT
FROM DUAL
)
CONNECT BY LEVEL <= END_DT - ST_DT
ORDER BY LEVEL
Cheers!!

Get last month data from first day until last day in Firebird

I am trying to query Firebird to get data from last month, from day 1 until last day (30 or 31 depending on the month). When I use the code below it gives me shifted dates from current, for example day 11/14/2017 until 12/13/2017.
The code:
WHERE DATE >= DATEADD(MONTH,-1, CURRENT_TIMESTAMP(2)) AND DATE<= 'TODAY'
The desired output is 11/01/2017 - 11/30/2017
What is the correct way to do it?
I don't use Firebird but I've used PostgreSQL fairly extensively and I think this should work:
WHERE
DATE BETWEEN dateadd(month, -1, CURRENT_DATE - EXTRACT(DAY FROM CURRENT_DATE) + 1)
AND CURRENT_DATE - EXTRACT(DAY FROM CURRENT_DATE)
Explanation
CURRENT_DATE - EXTRACT(DAY FROM CURRENT_DATE) + 1 should go back to the first of this month and dateadd with -1 month should take it to the previous month. Then if you're between CURRENT_DATE - EXTRACT(DAY FROM CURRENT_DATE) or in other words 12/13/2017 - 13 days that should be the last day of November. Crossing my fingers. Good luck.

Find how many days from begining of the month to current day (Oracle)

How do I determine how many days in a month, from the first to the current day (could be anywhere into the month). So if I have a field that gives 6/01/12 5:32:13 PM and 6/07/12 5:33:04 PM how do I get the difference?
I think something like this should do it:
SELECT TRUNC(SYSDATE) - TRUNC(SYSDATE,'MONTH') + 1 FROM DUAL
SELECT EXTRACT(DAY FROM sysdate) FROM dual;