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
);
Related
I have Start_Date(which is '2021-12-01') and End_Date(which is current calendar month)
I need to get COUNT(*) for each month from 2021-12-01 to the current month
Result set should look like this:
December, 2021 - 21
January, 2022 - 44
Feb, 2023 - 11
etc.
So what I tried(I only how do that manually):
DECLARE #datestamp DATE = '12/01/2021'
DECLARE #CurrentDate VARCHAR(100)
SET #CurrentDate = CONVERT(varchar, GETDATE(), 101)
SELECT COUNT(*) AS 'December, 2021'
FROM #TMP
WHERE Start_Date IS NOT NULL AND [Signup_Date] IS NOT NULL
AND Start_Date BETWEEN #datestamp AND '2021-12-31'
But I don't want to copy this query for each of 12 months. How do that dynamically?
I have a table with the following data:
user_id year week_number
1 2021 35
1 2021 27
1 2021 22
1 2021 50
1 2022 1
1 2022 7
1 2022 11
1 2022 15
1 2022 19
1 2022 2
1 2021 52
1 2022 3
1 2022 4
8 2021 48
8 2021 49
8 2021 50
8 2021 51
8 2021 52
8 2022 1
8 2022 2
8 2022 3
Now I want to get all weeks between year1, week1 and year2, week2. Variables could for example be:
-- Only get weeks from specific user. In this example its user 1.
userID = 1
year1 = 2021
week1 = 52
year2 = 2022
week2 = 05
I first had the following query:
-- Where 2021, 2022, 52 and 05 are the variables.
SELECT
*
FROM
[db].[dbo].[table]
WHERE
[user_id] = 1
AND
[year] BETWEEN '2021' AND '2022'
AND
[week_number] BETWEEN '52' AND '05'
This worked fine when I only had 1 year to select between. But in this example I use multiple years. Now week 52 belongs to year 2021 and week 05 to 2022. No result is now shown.
I also made a working query, but it asks extra parameters:
-- Here I have 2 extra week numbers I have to give.
SELECT
*
FROM
[db].[dbo].[table]
WHERE
[user_id] = 1
AND
([year] = '2021'
AND
[week_number] BETWEEN '52' AND '52'
OR
[year] = '2022'
AND
[week_number] BETWEEN '01' AND '05')
I don't think this is a very good solution because I would have to write an or for every extra year in between. I belive things could be simplified here, but dont know how.
So, is there a way to get the weeks that are between my variables? Without changing my table structure.
A small calculation may help:
...
WHERE
([user_id] = 1) AND
([year] * 100 + [week_number]) BETWEEN 202152 AND 202205
or
...
WHERE
([user_id] = 1) AND
(202152 <= [year] * 100 + [week_number]) AND
([year] * 100 + [week_number] <= 202205)
You could...use variables:
DECLARE #myUser int = 1,
#startYear int = 2021,
#endYear int = 2022,
#startWeek int = 5,
#endWeek INT = 13;
SELECT *
FROM [db].[dbo].[table]
WHERE [user_id] = #myUser
AND (
(#startYear = [year] AND #startWeek = [week_number] AND #startYear = #endYear AND #startWeek = #endWeek) --the selection is only one week
OR ([year] = #startYear AND [week_number] >= #startWeek AND [week_number] <= #endYear AND #startYear = #endYear AND #endWeek > #startWeek) --same year, multiple weeks
OR (#endYear > #startYear -- Spans multiple years
AND (
([year] >= #startYear AND [year] < #endYear AND [week_number] >= #startWeek ) --anything after start week for each year that is before the end year
OR ([year] <= #endYear AND [year] > #startYear AND [week_number] <= #endWeek) --anything before end week for any year after the start year
)
);
I have following columns from Jan to Dec:
Year - Jan - Feb - Mar - (etc.) - Dec
---- --- --- --- ---
2015 25 32 102 12
2016 30 40 50 60
How to effectively do SUM over past year? Let's say from GETDATE(), If today is 16.08.2017, I want SUM from 16.08.2016 (from august 2016 till august 2017).
I have following code:
select sum(val)
from t cross apply
(values (t.year, t.Jan, 1),
(t.year, t.Feb, 2),
. . .
) v(yyyy, val, mon)
where yyyy * 100 + mon >= (year(getdate()) - 1) * 100 + month(getdate());
which works, but is there any way to do it without cross apply? (for instance: just where clause)
how about something like below which uses UNPIVOT notation.
select sum(val)
from
(select * from t )s
unpivot
( val for month in ( Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec))up
where
cast(month+ cast(year as varchar(20)) as date)
between dateadd(m,-12,getdate()) and getdate()
Live Demo
Assuming all your column month names are 3 letters, you could also use dynamic SQL. Something like the following should work.
DECLARE #CurrentDate DATE = CAST(GETDATE() AS DATE)
DECLARE #Year INT = DATEPART(YEAR, #CurrentDate)
DECLARE #PrevYear INT = #Year - 1
DECLARE #Month CHAR(3)
SET #Month = convert(char(3), #CurrentDate, 0)
BEGIN
EXEC('SELECT SUM(t.'+#Month+') FROM t WHERE Year >= ' + #PrevYear + ' AND Year <= ' + #Year)
END
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
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.