How to filter the data based on particualr column for the current fiscal year in sql - sql

Fiscal Duration is : July to June I have a [Due Date] column in the
table. Now i need to filter the remaining columns data based on [Due
Date] column which is Current Fiscal Year Data. Here Requirement is i
should not do hardcode for getting the date, because if we entered in
to next fiscal year, we have to get next fiscal year data
automatically.
Please can any one can suggest either the DAX measure or Sql Query for
this logic.

Extract the year and month from your table by creating a view (ViewStep1) that uses those attributes:
TO_NUMBER (TO_CHAR ((DUE_DATE), 'YYYY')) AS YEAR
TO_NUMBER (TO_CHAR ((DUE_DATE), 'MM')) AS MONTH
Then you can use those columns to filter for each fisical year. Or even better, create another view that has a column FISICAL_YEAR which gets it´s data from a subselect of your previous view. Add a simple case in that subselect and your done:
select YEAR as YEAR, MONTH as MONTH, DUE_DATE,
(case
when MONTH >= 7 then YEAR
else (YEAR - 1) END) as FISICAL_YEAR
from ViewStep1 order by YEAR desc, MONTH desc, DUE_DATE desc;
If you have issues creating a view then I would advise to look into the documentation of your database system.

To get the fiscal year, just subtract six months. So the current fiscal year would be something like this:
where year(dateadd(month, -6, duedate)) = year(dateadd(month, -6, duedate))
You should be able to adapt this idea to your code.

Related

Generate custom start and end of months in SQL Server

I'm facing an issue while working with custom dates in T-SQL, we have a client that works with a different methodology of start and end of his month, instead of the default day 01 to start the month and ending in 31, 30 or 29, it's month start at day 26 and ends at 25 of the next month.
E.g., how usually is:
select sum(sales) as sales
from sales
where salesDate between '2020-09-01' and '2020-09-30'
-- or left(salesDate,7) = '2020-09'
Client custom month:
select sum(sales) as sales
from sales
where salesDate between '2020-08-26' and '2020-09-25' -- for setember
So, for this need, I have to calculate how many sales this client did from january until now, month per month, with this custom way... how can I do that?
Example of the query result I want to perform with this month determination:
This is a pretty awful situation. One method is to construct the first date of the month based on the rules:
select mon, sum(sales) as sales
from sales s cross apply
(values (case when day(salesdate) >= 26
then dateadd(month, 1, datefromparts(year(salesdate), month(salesdate), 1))
else datefromparts(year(salesdate), month(salesdate), 1)
) v(mon)
where v.mon >= '2020-01-01'
group by v.mon;
I would recommend adding the fiscal month column as a persisted computed column in the salesDate table so you can add an index and not have to worry about the computation.
Or, better yet, add a calendar table where you can look up the fiscal month for any (reasonable) date.

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

How to group dates as custom week numbers in SQL?

I have a series of email engagement dates, to create dashboard on QLIK. It has SQL Editor
I want to group a series of dates as Week 1, Week 2, and so on. My table has date column.
I am thinking along the lines for insert a column named "Week Number", based on the oldest date in the table, add 7 days range as week 1 and next 7 days range as Week 2 and so on.
In Qlik you can use the weekstart(Date) function or the week(Date) for just a week number. Either inthe script or as a calculated dimension in the chart.
Extra credit for year(Date)&'-'&week(Date) for 2019-23 etc
You can use datepart(wk, date_column) for grouping by week. You may want to add datepart(yy, date_column) to group by year and week.
You need to know the first day in your table was which day of the week, and then use the following script in SQL Server
declare #FirstDayOfTableWeekDay int = 2
SELECT CEILING( (CAST(ROW_NUMBER() OVER(ORDER BY [Date] ASC) AS float)+ CAST(#FirstDayOfTableWeekDay AS float)-1) / 7) AS WeekNumber
FROM YourTable

Trying to Look back 4 whole months in teradata with ADD_MONTHS function in sql statement

I'm trying to go back and retrieve counts for the last 4 full months. This is an example of what I have so far:
SELECT datecolumn, Count(datacolumnA) AS CountOfdatacolumnA, datacolumnB
FROM tableA
WHERE datacolumnB='AA' AND datecolumn >= ADD_MONTHS(CURRENT_DATE, -4)
My results show the last four months plus the current month, October in this case. The problem is that June isn't showing the correct count for the entire month. I'm only getting a partial count for the month.
You need to adjust to the start of the month. You can do this by subtracting the day of the month to get the '0th' of the month and then adding 1 to get the first. (I think dates in teradata are decimals with the int part being number of days since an epoch)
Select
datecolumn,
Count(datacolumnA) As CountOfdatacolumnA,
datacolumnB
From
tableA
Where
datacolumnB='AA' And
datecolumn >=
add_months(current_date, -4)
- extract(day from add_months(current_date, -4)) + 1

Sql daily comparison by month

I am writing a report for work which requires that I compare the amount of students dropped on a daily basis, what I mean is the report needs to show that on today the 5th of august X amounts of students dropped from the 1st to the 5th compared to X amount which dropped within the 1st to the 5th of July and so on for each Month of the year. Can anyone please help me by providing me with a query which I can use to have that info? thanks.
You want to compare the first number of days from a month. The following query gives you an example:
select yr, mon, count(*)
from (select extract(year from date) as yr, extract(month from date) as mon,
extract(day from date) as day
from t
) t
where day <= extract(day from now())
group by yr, mon
However, the exact syntax may depend on your database. For instance, the current date may be now(), getdate(), system_date or something else. Some databases don't support the extract, but most have a way to get the month and day of month.