how to get the month and year value for the give financial year - sql

how to get the month and year value for the give financial year
if my input is 2013.
my expected out put is
Month Year
4 2013
5 2013
6 2013
7 2013
8 2013
9 2013
10 2013
11 2013
12 2013
1 2014
2 2014
3 2014

Use Recursive CTE to get the expected result.
DECLARE #nYearInput AS CHAR(4)
DECLARE #start DATETIME, #end DATETIME
SET #nYearInput = '2013' -- Change your input here
SET #start = CAST(#nYearInput AS VARCHAR) + '0401'
SET #end= CAST(#nYearInput + 1 AS VARCHAR) + '0301'
;WITH cte AS
(
SELECT dt = DATEADD(DAY, -(DAY(#start) - 1), #start)
UNION ALL
SELECT DATEADD(MONTH, 1, dt)
FROM cte
WHERE dt < DATEADD(DAY, -(DAY(#end) - 1), #end)
)
SELECT YEAR(dt) AS Year, month(dt) AS month
FROM cte

Related

Subtract N from weeknum of current year

We have table WeeklyResults that looks like this:
Year WeekNum Value
2021 47 11.0
2021 48 14.0
2021 49 12.0
2021 50 17.0
2021 51 11.8
2021 52 11.3
2021 53 11.1
2022 01 11.5
2022 02 11.5
2022 03 81.5
We have a report with two parameters: Date and WeekNum. The report needs to show the last 6 weeks based on the weeknumber selected.
The issue is that, if user selects Week 2 of 2022, how can I subtract 6 weeks so that I get weeks 50, 51, 52, 53 of 2021 and weeks 1, 2 from 2022?
So, if the user selects 2022 and Week 02, it would show the last 6 weeks based on Weeknum 2 of year 2022 (wk50 to Wk02). If user selects 2021 and 52, it would show wk47-52.
You can use a little < and <= logic along side TOP and ORDER BY to achieve this:
DECLARE #Year int = 2022,
#WeekNum int = 3; --Note, if you are storing WeekNum as a (var)char,
--your leading zeros imply you are, then define the
--variable as a char(2).
SELECT TOP (6)
[Year],
WeekNum,
[Value]
FROM dbo.YourTable
WHERE ([Year] = #Year AND WeekNum <= #WeekNum)
OR [Year] < #Year
ORDER BY [Year] DESC,
WeekNum DESC;
Try this:
DECLARE #Year int = 2022
, #WeekNum varchar(02) = '02'
;
WITH FinalTable AS
(
SELECT TOP 6 *
FROM WeeklyResults
ORDER BY LTRIM(Year) + WeekNum DESC
)
SELECT *
FROM FinalTable
ORDER BY Year, WeekNum
Another option that doesn't involve an ORDER BY. Using DATEPART you can determine the final week of the prior year and subtract the number of weeks to get the records needed.
You may need to adjust what day is the first day of the week for your count. See this post for more info on that.
DECLARE
#Year INT = 2022
, #WeekNum INT = 3;
SELECT
*
FROM
WeeklyResults
WHERE
Year = #Year
AND WeekNum <= #WeekNum
OR
(
#WeekNum < 6
AND Year = #Year - 1
AND WeekNum > DATEPART (WEEK, CONCAT ('12/31/', #Year - 1)) - #WeekNum
);

Building a table of year and quarter combinations

Say I have a year and a quarter and I want to build a table lists a specific number of year and quarter combinations ending with the given year and quarter.
E.g.
**Input:**
Year = 2018
Quarter = 3
NumRows = 4
**Output:**
Year.....|Quarter
2018.....|3
2018.....|2
2018.....|1
2017.....|4
You can use recursive generation.
First you need to build a DATETIME object from the year and quarter.
DECLARE #Quarter INT = 3;
DECLARE #Year INT = 2018;
DECLARE #NumRows INT = 4;
DECLARE #initial_date DATETIME = DATEADD(quarter, #Quarter-1, DATEADD(year, #Year-1900, 0));
Then you can recursively generate the year & quarter combinations like this.
;WITH quarters AS (
SELECT #initial_date AS [qdate]
UNION ALL
SELECT DATEADD(quarter, -1, [qdate]) AS [qdate]
FROM quarters
WHERE [qdate] > DATEADD(quarter, -1*(#NumRows-1), #initial_date)
)
SELECT DATEPART(year, qdate) as year, DATEPART(quarter, qdate) as quarter FROM quarters
this uses a recursive cte
for
declare #year int = 2018,
#quarter int = 3,
#numrows int = 6
; with rcte as
(
select n = 1,
yr = #year,
qtr = #quarter
union all
select n = n + 1,
yr = yr - (((qtr - 1 + 4 - 1) % 4 + 1) / 4),
qtr = (qtr - 1 + 4 - 1) % 4 + 1
from rcte
where n < #numrows
)
select *
from rcte
order by n
/* result:
n yr qtr
1 2018 3
2 2018 2
3 2018 1
4 2017 4
5 2017 3
6 2017 2
*/
You can try to use cte recursive with some calculation
DECLARE #Year INT= 2018
DECLARE #Quarter INT = 3
DECLARE #NumRows INT= 10
;WITH CTE AS (
SELECT (#Year - #NumRows/4) yr,
4 - (#NumRows % 4) Quarter,
#NumRows NumRows
UNION ALL
SELECT yr + Quarter / 4,
CASE WHEN (Quarter + 1) % 4 = 0 THEN 4
ELSE (Quarter + 1) % 4
END,
NumRows- 1
FROM CTE
WHERE NumRows > 1
)
select yr,Quarter
from cte
sqlfiddle

Get Quarter and Year between two dates

I'd like to retrieve the list of years and quarters between two dates.
For example, from 25/12/2015 to 06/30/2017, the result should look like:
Year Quarter
2015 4
2016 1
2016 2
2016 3
2016 4
2017 1
2017 2
2017 3
You can use a tally table to do this.
declare #start date='2015-12-25';
declare #end date = '2017-06-30';
select distinct year(dateadd(day,rnum,#start)) yr,
datepart(quarter,dateadd(day,rnum,#start)) qtr
from (select row_number() over(order by (select null)) as rnum
from master..spt_values) t
where dateadd(day,rnum,#start) <= #end;
If you need to span more than 6 years... virtually identical to vkp (he's so fast!)
Declare #Date1 date = '2015-12-25'
Declare #Date2 date = '2017-06-30'
Select Distinct
[Year] =DatePart(YEAR,D)
,[Quarter]=DatePart(QUARTER,D)
From (
Select Top (DateDiff(DD,#Date1,#Date2)+1) D=DateAdd(DAY,-1+Row_Number() Over (Order By (Select Null)),#Date1)
From master..spt_values n1,master..spt_values n2
) A
Returns
Year Quarter
2015 4
2016 1
2016 2
2016 3
2016 4
2017 1
2017 2
Declare #StartDate Date='2016-01-01'
, #EndDate Date='2017-05-01'
DECLARE #Date TABLE
(
[Year] INT,
[Quarter] INT
)
WHILE #StartDate <= #EndDate
BEGIN
INSERT INTO #Date
([Year],
[Quarter])
SELECT DATEPART(YEAR,#StartDate) AS [Year],
CASE WHEN DATEPART(MM,#StartDate) BETWEEN 1 AND 3 THEN 1
WHEN DATEPART(MM,#StartDate) BETWEEN 4 AND 6 THEN 2
WHEN DATEPART(MM,#StartDate) BETWEEN 7 AND 9 THEN 3
WHEN DATEPART(MM,#StartDate) BETWEEN 10 AND 12 THEN 4
END AS [Quarter]
SET #StartDate = DATEADD(DAY,1,#StartDate)
END
SELECT DISTINCT [Year],[Quarter] FROM #Date

SQL - Create a temp table or CTE of first day of the month and month names

I need to create a temp table or common table expression based on 2 paremters in a SQL Server 2012 environment
#calYear
#currentYear
so if #calYear = 5 and #currentYear='2014'
I would like to generate a temp table of 5 years starting from current year with 4 columns like
YearDesc MonthName MonthNum FirstDayOfMonth
2014 Jan 1 1/1/2014
2014 Feb 2 2/1/2014
2014 Mar 3 3/1/2014
...
...
...
2018 Oct 10 10/1/2018
2018 Nov 11 11/1/2018
2018 Dec 12 12/1/2018
Is it possible to do a Do While loop efficently? How would I account for the month names?
I'm using a really cumbersome Do While loop to iterate all the months of the year then iterate all the years.
One way using a recursive cte:
declare #calYear int = 5, #currentYear char(4) = '2014'
;with cte (dt) as (
select DATEFROMPARTS(#currentyear,1,1) dt
union all
select dateadd(month,1,dt)
from cte where dt < dateadd(year,#calyear,DATEFROMPARTS(#currentyear,1,1))
)
select year(dt) YearDesc, datename(month, dt) MonthName, month(dt) MonthNum, dt FirstDayOfMonth
from cte
order by dt
Or using a numbers table: (in this case master..spt_values)
declare #calYear int = 5, #currentYear char(4) = '2014'
;with cte2 (dt) as (
select dateadd(month,number,DATEFROMPARTS(#currentyear,1,1)) dt
from master..spt_values where type = 'p'
and number <= 12*#calYear
)
select year(dt) YearDesc, datename(month, dt) MonthName, month(dt) MonthNum, dt FirstDayOfMonth
from cte2
order by dt

SQL days available using a not exists on current financial year

I have a table called 'holiday table' which basically contains dates for all days where employees will not be in work (e.g bank holidays etc)
The below query is basically looking at the current financial year and working out how many days are available firstly by month, and then using the unuion all cummulatively, (e.g April-May, April-June) I dont need one for April though as I can use the non-cumulative for this.
See query:
DECLARE #StartDate DATETIME,
#EndDate DATETIME
--available days
--current – start of this financial year
SELECT #StartDate = (select
case when month(getdate()) >= 4 then
convert(datetime, cast(year(getdate()) as varchar) + '-4-1')
else
convert(datetime, cast(year(getdate())-1 as varchar) + '-4-1')
end),
--current – end of this financial year
#EndDate = (select
case when month(getdate()) < 4 then
convert(datetime, cast(year(getdate()) as varchar) + '-3-31')
else
convert(datetime, cast(year(getdate())+1 as varchar) + '-3-31')
end)
CREATE TABLE #data
(
firstday DATETIME NOT NULL PRIMARY KEY,
workingdays INT NOT NULL
);
WITH dayscte ([Date])
AS (SELECT #StartDate
UNION ALL
SELECT Dateadd(DAY, 1, [Date])
FROM dayscte
WHERE [Date] <= #Enddate)
INSERT INTO #data
SELECT MIN([Date]),
COUNT(*) [Day]
FROM dayscte
LEFT JOIN dbo.Holiday_Table
ON [Date] BETWEEN dbo.Holiday_Table.sch_cal_d AND dbo.Holiday_Table.sch_cal_ed
where
NOT EXISTS (
SELECT sch_id,sch_cal_d,sch_cal_ed FROM dbo.Holiday_Table WHERE
sch_id ='66291100Ks'
AND
[date] <= sch_cal_d
AND
[date] >= sch_cal_ed
)
AND Datename(weekday, [Date]) NOT IN ( 'Saturday', 'Sunday' )
GROUP BY Datepart(MONTH, [Date]),
Datepart(YEAR, [Date])
OPTION (MAXRECURSION 366)
DECLARE #Date DATETIME
SET #Date = (SELECT MIN(firstday)
FROM #data)
SELECT Period,
workingdays [Days_Available_Minus_Holidays] ,
year (firstday) AS [Year]
FROM (SELECT Datename(MONTH, firstday) [Period],
workingdays,
0 [SortField],
firstday
FROM #data
UNION
SELECT Datename(MONTH, #Date) + '-' + Datename(MONTH, firstday),
(SELECT SUM(workingdays)
FROM #data b
WHERE b.firstday <= a.firstday ) [WorkingDays],
1 [SortField],
firstday
FROM #data a
WHERE
firstday > #Date) data
ORDER BY sortfield,
firstday
DROP TABLE #data
GO
The results for this are as follows:
Period Days_Available_Minus_Holidays Year
April 19 2012
May 22 2012
June 19 2012
July 22 2012
August 22 2012
September 20 2012
October 23 2012
November 22 2012
December 19 2012
January 23 2013
February 20 2013
March 21 2013
April 1 2013
April-May 41 2012
April-June 60 2012
April-July 82 2012
April-August 104 2012
April-September 124 2012
April-October 147 2012
April-November 169 2012
April-December 188 2012
April-January 211 2013
April-February 231 2013
April-March 252 2013
April-April 253 2013
My problem is when I get to the cumulative, it does another 'April' and then at the bottom it does an 'April-April' I do not need a cumulative for April as it is only one month do basically I dont want the first or last cumulative values as April is covered by the non-cumulatives, or if the second 'April' must stay, then it should not read '1' as days available, by be the same as the non-cumulative, which is 19 as this is how many days are actually available.
Try removing the equals in your WITH clause
Change WHERE [Date] <= #Enddate to WHERE [Date] < #Enddate
It seems your adding a day to the date before the WHERE clause therefore it is overstepping by a day.