How to calculate Last Week of Month by WeekNO and Year in SQL - sql

I want to calculate Last Week Number of Month in SQL. I am having Week Number and Year.
Eg. If I pass WeekNo=51 , Year=2008 , than function should return LastWeekofMonth= 52.
I want to calculate Week number using below standards.
According to ISO 8601:1988 that is used in Sweden the first week of the year is the first week that has at least four days within the new year.
So if your week starts on a Monday the first Thursday any year is within the first week. You can DateAdd or DateDiff from that.
Please Help me..........
Thanks in advance.

SELECT WEEK(LAST_DAY(STR_TO_DATE('2008-51-Mon', '%x-%v-%a')));
Should do the trick for getting the last week number of month with MySQL :
I first convert to a date, then I get the last day of the month (here: 2008-12-31), then I compute the week of the last day of the month (52).
It should be easy to turn it into a function.
Hope this helps.

This is fairly straightforward if you use a calendar table. The month you need is given by this query.
select iso_year, month_of_year
from calendar c
where iso_year = 2008 and iso_week = 51
group by iso_year, month_of_year
--
iso_year month_of_year
2008 12
So you can use that result in a join on the calendar table, like this.
select max(c.iso_week) as last_week_of_month
from calendar c
inner join
(select iso_year, month_of_year
from calendar c
where iso_year = 2008 and iso_week = 51
group by iso_year, month_of_year) m
on m.iso_year = c.iso_year and m.month_of_year = c.month_of_year;
--
last_week_of_month
52
Here's one example of a calendar table, but it's pretty thin on CHECK constraints.

If you're using SQL Server, you can perform a calculation by using a master table, without creating a calendar table. This fellow gives you a very good explanation, which I recommend that you read. His SQL for calculating the first and last Sundays of each month can be adapted for your use:
declare #year int
set #year =2011
select min(dates) as first_sunday,max(dates) as last_sunday from
(
select dateadd(day,number-1,DATEADD(year,#year-1900,0))
as dates from master..spt_values
where type='p' and number between 1 and
DATEDIFF(day,DATEADD(year,#year-1900,0),DATEADD(year,#year-1900+1,0))
) as t
where DATENAME(weekday,dates)='sunday'
group by DATEADD(month,datediff(month,0,dates),0)
Edit: Once you have the date of the Thursday, you can get the week number from that date like this:
DECLARE #Dt datetime
SELECT #Dt='02-21-2008'
SELECT DATEPART( wk, #Dt)

Related

In Bigquery SQL: How to fetch previous week, specified week and next week data?

Scenario: From bigquery, have to fetch the specified date's week data + its previous week data + its next future week data. Week starts is Wednesday.
Tried Query:
Select * from table
and extract(week(wednesday) from Calendar_Day) >= (extract(week(wednesday) from PARSE_DATE('%d/%m/%Y','21/10/2020')) - 1)
and extract(week(wednesday) from Calendar_Day) >= (extract(week(wednesday) from PARSE_DATE('%d/%m/%Y','21/10/2020') ))
and extract(week(wednesday) from Calendar_Day) <= (extract(week(wednesday) from PARSE_DATE('%d/%m/%Y','21/10/2020')) + 1)
But this is not working for me.
Need help in resolving this. Thanks in Advance!
EXTRACT the week as the code already does. and the year as the weeks repeat every year.
GROUP BY the week and year. At this point I find it handy to make a STRUCT from the remaining fields as it simplifies the remaining code.
make another query that uses the query which did the GROUP BY, I used a WITH. In this last query, LEAD and LAG the data with a WINDOW by week.
Here's an example from a public dataset.
WITH
data_by_week AS (
SELECT
EXTRACT(year FROM date) AS year,
EXTRACT(week(wednesday) FROM date) AS week,
struct(
SUM(new_tested) as total_new_tested,
sum(new_recovered) as total_new_recovered
) as week_data
FROM
`bigquery-public-data.covid19_open_data.covid19_open_data`
GROUP BY
year,
week )
SELECT
year,
week,
LAG(week_data) OVER window_by_week AS previous_week,
week_data AS current_week,
LEAD(week_data) OVER window_by_week AS following_week
FROM
data_by_week
WINDOW
window_by_week AS ( ORDER BY year, week)
ORDER BY
year,
week

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

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);

Get date for nth day of week in nth week of month

I have a column with values like '3rd-Wednesday', '2nd-Tuesday', 'Every-Thursday'.
I'd like to create a column that reads those strings, and determines if that date has already come this month, and if it has, then return that date of next month. If it has not passed yet for this month, then it would return the date for this month.
Expected results (on 4/22/16) from the above would be: '05-18-2016', '05-10-2016', '04-28-2016'.
I'd prefer to do it mathematically and avoid creating a calendar table if possible.
Thanks.
Partial answer, which is by no means bug free.
This doesn't cater for 'Every-' entries, but hopefully will give you some inspiration. I'm sure there are plenty of test cases this will fail on, and you might be better off writing a stored proc.
I did try to do this by calculating the day name and day number of the first day of the month, then calculating the next wanted day and applying an offset, but it got messy. I know you said no date table but the CTE simplifies things.
How it works
A CTE creates a calendar for the current month of date and dayname. Some rather suspect parsing code pulls the day name from the test data and joins to the CTE. The where clause filters to dates greater than the Nth occurrence, and the select adds 4 weeks if the date has passed. Or at least that's the theory :)
I'm using DATEFROMPARTS to simplify the code, which is a SQL 2012 function - there are alternatives on SO for 2008.
SELECT * INTO #TEST FROM (VALUES ('3rd-Wednesday'), ('2nd-Tuesday'), ('4th-Monday')) A(Value)
SET DATEFIRST 1
;WITH DAYS AS (
SELECT
CAST(DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),N.Number) AS DATE) Date,
DATENAME(WEEKDAY, DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),N.Number)) DayName
FROM master..spt_values N WHERE N.type = 'P' AND N.number BETWEEN 0 AND 31
)
SELECT
T.Value,
CASE WHEN MIN(D.Date) < GETDATE() THEN DATEADD(WEEK, 4, MIN(D.DATE)) ELSE MIN(D.DATE) END Date
FROM #TEST T
JOIN DAYS D ON REVERSE(SUBSTRING(REVERSE(T.VALUE), 1, CHARINDEX('-', REVERSE(T.VALUE)) -1)) = D.DayName
WHERE D.Date >=
DATEFROMPARTS(
YEAR(GETDATE()),
MONTH(GETDATE()),
1+ 7*(CAST(SUBSTRING(T.Value, 1,1) AS INT) -1)
)
GROUP BY T.Value
Value Date
------------- ----------
2nd-Tuesday 2016-05-10
3rd-Wednesday 2016-05-18
4th-Monday 2016-04-25

Create date of a specific weekday two weeks before the current week

I'm trying to create a SQL statement (in H2 Dialect) that creates a DATE which is
the tuesday in the "week-of-year" that is two weeks from the current week.
For example, given CURRENT_DATE() returns 2014-04-23, the date that I want would be 2014-04-08, which is the Tuesday in the week that is two weeks before the week the Date "2014-04-23" is in.
How can I express this with H2's Date and Time functions?
In sql-server I would do it like this
DECLARE #date DATE = GETDATE();
SELECT DATEADD(DAY,3-DATEPART(WEEKDAY,#date),DATEADD(WEEK,-2,#date))
Where '3' represents day of the week (Tuesday) and '-2' represents subtracting two weeks.
So now in h2 it is very similar
SELECT DATEADD('DAY',3-DAY_OF_WEEK(CURRENT_DATE()),DATEADD('WEEK',-2,CURRENT_DATE()))