Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


query to get dates for a number of weeks


query to get dates for a number of weeks

Author
Message
chrismat
chrismat
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 593
I need a query that will produce rows of dates given a starting date and incrementing that date for a number of weeks (also given on the same source date row).

DDL and some data:
create table Dates (
UserID int not null,
InDate date not null,
Weeks int not null);

insert into Dates (UserID,InDate, Weeks)
select 1,'8/31/2012',5 union
select 2,'9/14/2012',2;

Expected results:
UserID,OutDates
1,9/7/2012
1,9/14/2012
1,9/21/2012
1,9/28/2012
1,10/5/2012
2,9/21/2012
2,9/28/2012
chrismat
chrismat
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 593
That's a lot of great info Celko, but I feel it's way overthought for my situation. This isn't for a business nor do standards or performance matter. I suppose at this point it's academic: what would a possible query look like?
chrismat
chrismat
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 593
how about a query without setting up all that extra stuff... using only my original ddl and sample data.
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24207 Visits: 37978
Something like this perhaps?



--DDL and some data:
create table Dates (
UserID int not null,
InDate date not null,
Weeks int not null);

insert into Dates (UserID,InDate, Weeks)
select 1,'8/31/2012',5 union
select 2,'9/14/2012',2;

--Expected results:
--UserID,OutDates
--1,9/7/2012
--1,9/14/2012
--1,9/21/2012
--1,9/28/2012
--1,10/5/2012
--2,9/21/2012
--2,9/28/2012

with cteTally(n) as (
select top (select max(d1.Weeks) from Dates d1)
row_number() over (order by (select null)) as n
from
sys.all_columns a
cross join sys.all_columns b
)
select
d.UserID,
dateadd(wk, t.n, d.InDate) OutDates
from
Dates d
cross join cteTally t
where
t.n <= d.Weeks
order by
d.UserID,
OutDates
;
go

drop table Dates;
go




Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search