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

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

Related

Specific Month-day select statement query

I would like to select from a table where the date falls within a specific time each year f.e:
select * from Customer where date >= August 15th and date <= December 20th
Since this will be for a report that runs every year, I do not want to hardcore the date as I will have to change it every year. I would like to have it dynamic to pick the date from August 15th to December 20th of the current year.
I have the below query where I can retrieve the Month and Date:
SELECT DATENAME(month, date) AS Month,DATENAME(day, date) AS Day from Customer
However, I am struggling to have this selection date range.
TIA
In SQL SERVER 2017
maybe this can help you:
SELECT * FROM Customer WHERE date BETWEEN
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'08','15')) AND
CONVERT(DATETIME,CONCAT(YEAR(GETDATE()),'12','20'))
This add the current year to a concatenated date you want, then convert it all into datetime type..

Select subscription date range

How to turn a date into last month's date?
SELECT extract(day from (Select (period_start) from accounts where id = 'id')) AS "Day of month";
The above give me the day of the month
Account Table
id (primary)
startPeriod
User records
timestamp
account_id (foreign key)
The user is registered at 02-12for subscription start
I would like to have a query to select the date range from the current month to last month
This current month is April, so I would like to select the record that is
select records from table where date > '03-12-2022'
get the subscription start period and get the date
set the query limited to last month with the subscription start date
how to produce this date 03-12-2022 which is driven by subscription start period and previous month?
many thanks
This approach should work:
Get the number of months between the subscription date and today e.g. age(timestamp1, timestamp2)
Get the number of months and subtract 1 to get last month e.g. duration = extract(month from age(timestamp1, timestamp2)) - 1
Add the months to the subscription date e.g. (subscription_date + (duration || ' month')::INTERVAL)

How to find previous month's last day for last year in Teradata SQL

I want to find out the previous month's last day, but I want it for last year, in Teradata SQL.
Examples:
If current date is '2022-02-02', then the output I require is '2021-01-31'
If current date is '2022-07-25', then the output I require is '2021-06-30'
Substract 13 months from today and then find the last day of this month:
last_day(add_months(current_date, -13)

How could I get the date of the week, month or year starting from the first day up to the current day of the current week, month or year?

I'm sorry if the title is confusing.
Basically I am creating a dashboard using Oracle Apex 18.x and in one my card, I am comparing the sales between today vs yesterday, this week vs last week, this month vs last month and this year vs last year.
I have no problem with the today vs yesterday. My issue is with the week, month and year because I want to compare only up to the current day, not the whole last week.
For example, if today is the 4th day of the current week, then I'd have to compare it from last week up to its 4th day only as well.
For the month, if let's say today is 25-Mar-2018 then I should only compare it to the month of Feb from 1-25 only. Same with the year.
It wouldn't be a problem if the requirement is to get the full last week, full last month and full last year.
For week i would do it like:
select
TRUNC(sysdate, 'DAY') start_of_the_week,
(sysdate) day_of_current_week,
(TRUNC(sysdate, 'DAY') - 7 ) start_of_last_week
(sysdate - 7) day_of_last_week,
from dual;
For month it would be:
select
(sysdate) day_of_current_month,
TRUNC(sysdate, 'mm') month_start_date,
ADD_MONTHS(sysdate,-1) day_of_last_month,
ADD_MONTHS(TRUNC(sysdate, 'mm'),-1) start_of_last_month
from dual;

local week of the year in postgresql

I have to generate year wise, weekly reports for some data. Now When I aggregate date on week number, and week number is calculated from extract from creation date.
Now the problem is these both queries return week number 52.
SELECT EXTRACT(WEEK FROM TIMESTAMP '2006-01-01');
SELECT EXTRACT(WEEK FROM TIMESTAMP '2006-12-31');
First query return 52 (52nd week of 2005) and 2nd query return 52 (52nd week of year 2006). thats documented behavior.
But I want to Calculate local week number, and results for first query should be 1 and other query would return 53.
You can't do this with the exctract() function, it only supports ISO weeks.
But the to_char() function has an option for this:
SELECT to_char(DATE '2006-01-01', 'WW')::int` --> 1
SELECT to_char(DATE '2006-12-31', 'WW')::int` --> 53
For date 2006-01-01 end week is start in 2005 year, that same problem is 1999 year.
Clausule EXTRACT(WEEK getting year where week is started not ending.
You can use this code:
SELECT floor(EXTRACT(doy FROM TIMESTAMP '2006-01-01')/7 + 1);
SELECT floor(EXTRACT(doy FROM TIMESTAMP '2006-12-31')/7 + 1);