Oracle RPD Ago function SQL equivalent - sql

I am looking for a function in SQL that can help me compute an aggregate for same period last month or same period last year. For example, Today is 14th November and my current revenue month to date is 1000$. I am looking for a function in SQL that can sum up revenue for for the previous month i.e sept but only for the same period of 14 days. Is there a function that can achieve this??
The AGO function in OBIEE (RPD) does this with ease, I am looking to do this using SQL. Any ideas?

That could be as simple as
SELECT sum(gains)
FROM business_transactions
WHERE billing_date
BETWEEN date_trunc('month', current_timestamp - INTERVAL '1 month')
AND current_timestamp - INTERVAL '1 month');

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

SQL Presto - Week function to star on a Sunday

I'm trying to extract the week number from a date, and I want the week to be counted from Sunday to Saturday. This is what I currently have, but I can't seem to find any solution for this is SQL Presto.
SELECT WEEK(date) AS weeknum
Can this be solved?
Thank you!
One method is:
select week(date + interval '1 day') - interval '1 day'
Note: This may not work on the last day of the year.
Alternatively you can use the MySQL-like functions:
select date_format(date, '%V')
This has the week starting on Sunday.

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 query to get week number from date while setting week starting from Sunday

I am trying to run a query in AWS Athena to get the week number from a date. I need the week to start on Sunday.
I have tried the below query and it works but it considers the week starts from Monday.
Extract(week from date) as week_number
Google bigquery has an option where you can specify the week start. Eg:
Extract(week(Sunday) from date) as week_number
Is there something similar in Athena?
Request assistance.
Thank you.
If there isn't, you can add one day:
extract(week from date + interval '1' day) as week_number

Previous year Year To Date with partial current year Year To Date Calculation SQL Server

I have a revenue table with data for last year and current year. I need to calculate the YTD last year and YTD current year, BUT I need to only consider data from min(date) from last year PER branch for current year YTD calculation.
eg: Branch KTM has data from 2018-02-25 not from Jan 1st.
Now I want to get YTD for the current year from the same date on 2019 till today.
I am able to get whole YTD for last year and this year, and also the minimum date/weeknumber for each branch for last year, but unable to calculated partial YTD for the current year.
Here is one drive link to mydata and sql : https://1drv.ms/u/s!Ave_-9o8DQVEgRS7FaJmm48UNsWz?e=lRfOJF
A snippet from my code
I need help with the SQL query to do this.
This returns the number of days between the same day-of-year of a last year's date and today's date:
select current_date - (date'2018-02-25' + interval '1' year); -- PostgreSQL
select datediff(current_date, (date'2018-02-25' + interval '1' year)); -- MySQL
Alternative version:
select extract(doy from current_date) - extract(doy from date'2018-02-25'); -- PostgreSQL
doy stands for day of year. At the time of the answer (2019-09-24) all queries return 211.
To sum values in that date range, use BETWEEN:
SELECT sum(revenue)
FROM your_table
WHERE date BETWEEN date'2018-02-25' + interval '1' year AND current_date