Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

query to get dates for a number of weeks Expand / Collapse
Author
Message
Posted Saturday, September 29, 2012 12:42 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 3:55 PM
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

Post #1366210
Posted Saturday, September 29, 2012 4:54 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 3:55 PM
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?
Post #1366219
Posted Saturday, September 29, 2012 6:38 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, October 22, 2014 3:55 PM
Points: 22, Visits: 593
how about a query without setting up all that extra stuff... using only my original ddl and sample data.
Post #1366225
Posted Saturday, September 29, 2012 10:52 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 10:23 PM
Points: 21,921, Visits: 34,685
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





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)
Post #1366229
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse