Adrian
Adrian

Reputation: 346

MSSQL - split records per week_start and week_end

I have a table similar to the one represented below.

myID |  some data  |  start_date   |    end_date

1        Tom          2016-01-01       2016-05-09
2        Mike         2015-03-01       2017-03-09
...

I have a function that when provided with start_date, end_date, interval (for example weeks)

returns me data as below. (splits the start and end dates to week intervals)

select * from my_function('2016-01-01','2016-01-12', 'ww')

2015-12-28 00:00:00.000  |  2016-01-03 00:00:00.000 15W53
2016-01-04 00:00:00.000  |  2016-01-10 00:00:00.000 16W1    
2016-01-11 00:00:00.000  |  2016-01-17 00:00:00.000 16W2    

I would like to be able to write a query that returns all of the values from the 1 table, but splits Start date and end date in to multiple rows using the function.

 myID |  some data  |  Week_start_date   |    Week_end_date   | (optional)week_num

    1        Tom         2015-12-28           2016-01-03        15W53
    1        Tom         2016-01-04           2016-01-10        16W1
    1        Tom         2016-01-11           2016-01-17        16W2
    ...
    2        Mike         etc....

Could someone please help me with creating such a query ?

Upvotes: 0

Views: 72

Answers (2)

Chanukya
Chanukya

Reputation: 5893

select myID,some_data,b.Week_start_date,b.Week_end_date,b.(optional)week_num from #a cross apply
(select * from my_function('2016-01-01','2016-01-12', 'ww'))b

like sample data i tried

create table #a
(
myID int, some_data varchar(50) , start_date  date,   end_date date)
insert into #a values


(1,'Tom','2016-01-01','2016-05-09'),
(2,'Mike','2015-03-01','2017-03-09')

here iam keeping function result into one temp table

create table #b
(
a datetime,b datetime, c varchar(50)
)
insert into #b values
('2015-12-28 00:00:00.000','2016-01-03 00:00:00.000','15W53'),
('2016-01-04 00:00:00.000','2016-01-10 00:00:00.000','16W1 '),   
('2016-01-11 00:00:00.000','2016-01-17 00:00:00.000','16W2 ')

select myID,some_data,b.a,b.b,b.c from #a cross apply
(select * from #b)b

output like this

myID    some_data   a   b   c
1   Tom 2015-12-28 00:00:00.000 2016-01-03 00:00:00.000 15W53
1   Tom 2016-01-04 00:00:00.000 2016-01-10 00:00:00.000 16W1 
1   Tom 2016-01-11 00:00:00.000 2016-01-17 00:00:00.000 16W2 
2   Mike    2015-12-28 00:00:00.000 2016-01-03 00:00:00.000 15W53
2   Mike    2016-01-04 00:00:00.000 2016-01-10 00:00:00.000 16W1 
2   Mike    2016-01-11 00:00:00.000 2016-01-17 00:00:00.000 16W2 

Upvotes: 2

TheGameiswar
TheGameiswar

Reputation: 28920

Based on your current result and expected result,the only difference ,i see is myID

so you will need to frame your query like this..

;with cte
as
(
select * from my_function('2016-01-01','2016-01-12', 'ww')
)
select dense_rank() over (order by somedata) as col,
* from cte

Dense Rank assigns same values for the same partition and assigs the sequential value to next partition ,unlike Rank

Look here for more info:
https://stackoverflow.com/a/7747342/2975396

Upvotes: 0

Related Questions