Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i have a table with two columns :
First : Occurence, containing dates
Second : the number of the week from the date

I'd like to do a Select on my table, and get only dates every tuesday and every n weeks(6 for example) from the first occurence.
SQL
Ex: 
OCCURENCE     WEEK_NUMBER
07/03/2017         8        --First tuesday
14/03/2017         9        -- Second tuesday of the month
21/03/2017        10        --Third tuesday of the month
28/03/2017        11        --Fourth
   ...           ...

So the first occurence of my task is on the 07/03/2017 and i would like to get every occurence every 3 weeks , so get as return of the select :

SQL
OCCURENCE     WEEK_NUMBER
07/03/2017         8          --First occurence
28/03/2017        11          -- Third occurence
   ...           ...

It must work if i want to get every n weeks ^^

Thanks :)

What I have tried:

I tried to use modulo on week_number but i don't know how to get only occurences for the n week
Posted
Updated 7-Mar-17 6:22am
v3
Comments
Andy Lanng 7-Mar-17 10:32am    
What sql language? T-SQL, P-SQL, MySQL...
Vincent Ducroquet 7-Mar-17 10:35am    
SQL-Server or T-SQL

SQL
declare @weeks int
set @weeks = 3

--Common Table Expressions
;with mycte as (
	-- get the lower and upper bounds to limit the recursion
	select min([table_date]) as startPoint, max([table_date]) as endPoint, [id]
	from [table]
	group by id
), mycte2 as (
	--recursive cte gets all dates that are @weeks after the startPoint and are lessthan or equal to the endPoint
	select startPoint, endPoint, id
	from mycte
	--These are very powerful and fast
	union all select DATEADD(week,@weeks,startPoint), endPoint, id
	from mycte2
	where DATEADD(week,@weeks,startPoint) <= endPoint
)
--finally, select all the items that fit into the id, and date and date + n*m weeks
select * from [table] t
inner join mycte2 m on t.id = m.id and t.[table_date] = m.date


There is a lot going on here. Here are some things to look into
Using Common Table Expressions[^]
Recursive Queries Using Common Table Expressions[^]

UPDATE: Whoops. "Union all" is the correct syntax
 
Share this answer
 
v2
Comments
Andy Lanng 7-Mar-17 10:58am    
I may have missed 3 letters but that was all from memory. Sheesh I need a break
Vincent Ducroquet 7-Mar-17 11:47am    
Ahah thank you ! i'm trying to use your code but it's is difficult because my table is generated by a function, which is contained in a DLL and only generate one column(Occurence), so i'm trying to add an id column and week_number column^^
Andy Lanng 7-Mar-17 11:48am    
Show me what you have and I might be able to help
Try this:
SELECT * FROM tablename WHERE DATEDIFF(week, CONVERT(DATE,'07/03/2017',103), CONVERT(DATE,occurence,103) ) % 3 = 0
or demo[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900