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? - sql

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;

Related

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 to get the week before the current week?

I'm trying to compare the week number of a started task with the week before the current one.
and to_char(t.plan_start_dttm, 'YYYY-IW') = to_char(sysdate-1, 'YYYY-IW')
So yesterday, it was working fine, I got all records of last week. But today, I get the ones of this week.
So apparently, my "sysdate -1 is not the right way to do it.
Thank you
yesterday, it was working fine, I got all records of last week.
Yesterday was Monday of this week; so subtracting one day would be Sunday of last week and truncating to the start of the week would be Monday of last week.
Today is Tuesday; so subtracting one day would be Monday of this week and then truncating to the start of the week would still be this week.
Instead, subtract an entire week's worth of days (7):
and t.plan_start_dttm >= TRUNC( sysdate - 7, 'IW')
and t_plan_start_dttm < TRUNC( sysdate, 'IW' )
and if you compare on a range then Oracle can use an index on the plan_start_dttm column; whereas if you use TO_CHAR or TRUNC on the column then the column index cannot be used and you would need a separate function-based index.
Subtract 7 days instead of 1. Or 1 week. I also prefer trunc():
trunc(t.plan_start_dttm, 'IW') = trunc(sysdate - interval '7' day, 'IW')
There is no need to convert to strings for this purpose.

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

Teradata SQL Same Day Prior Year in same Week

Need help figuring out how to determine if the date is the same 'day' as today in teradata. IE, today 12/1/15 Tuesday, same day last year was actually 12/2/2014 Tuesday.
I tried using current_date - INTERVAL'1'Year but it returns 12/1/2014.
You can do this with a bit of math if you can convert your current date's "Day of the week" to a number, and the previous year's "Day of the week" to a number.
In order to do this in Teradata your best bet is to utilize the sys_calendar.calendar table. Specifically the day_of_week column. Although there are other ways to do it.
Furthermore, instead of using CURRENT_DATE - INTERVAL '1' YEAR, it's a good idea to use ADD_MONTHS(CURRENT_DATE, -12) since INTERVAL arithmetic will fail on 2012-02-29 and other Feb 29th leap year dates.
So, putting it together you get what you need with:
SELECT
ADD_MONTHS(CURRENT_DATE, -12)
+
(
(SELECT day_of_week FROM sys_calendar.calendar WHERE calendar_date = CURRENT_DATE)
-
(SELECT day_of_week FROM sys_calendar.calendar WHERE calendar_date = ADD_MONTHS(CURRENT_DATE, -12))
)
This is basically saying: Take the current dates day of week number (3) and subtract from it last years day of week number (2) to get 1. Add that to last year's date and you'll have the same day of the week as current date.
I tested this for all dates between 01/01/2010 and CURRENT_DATE and it worked as expected.
Why don't you simply subtract 52 weeks?
current_date - 364
The SQL below will get you to the abbreviated name for the day of week, it's cumbersome but it works across versions of Teradata.
SELECT CAST(CAST(ADD_MONTHS(CURRENT_DATE, -12) AS DATE FORMAT 'E3') AS CHAR(3)) AS LY_DayOfWeek
, CAST(CAST(CURRENT_DATE) AS DATE FORMAT 'E3') AS CHAR(3)) AS CY_DayOfWeek
Dates are internally represented at integers in Teradata as (Year-1900) * 100000 + (MONTH * 100) + DAY. You may be able to do some creative arithmetic to figure out that 12/1/2015 Tuesday was 12/2/2014 Tuesday last year.

SSRS BIDS 2008 Expression

What is the correct expression to use for todays date plus 1 year.
I assume it starts with Now()+ but im unsure from there
This page has lots of great examples, including:
=DateAdd(DateInterval.Month, 6, Parameters!StartDate.Value)
From that and the example before it, it looks like you want:
=DateAdd(DateInterval.Year, 1, Today())
this should be what your looking for:
--midnight last day of last month
select DateAdd(mm,-0,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
--midnight last day of this month
select DateAdd(mm,+1,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
--midnight last day of last month 1 year ago
select DateAdd(yy,-1,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))))
--midnight last day of this month 1 year ago
select DateAdd(yy,-1,DateAdd(mm,+1,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))))