|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, October 26, 2012 3:17 PM
Points: 22,
Visits: 591
|
|
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
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
>> I need a query that will produce rows of dates given a starting date and incrementing that date for a number of weeks <<
You got the date display format wrong; ANSI/ISO Standards use the ISO-8601 format (yyyy-mm-dd). Build a calendar table with one column for the calendar date and other columns to show whatever your business needs in the way of temporal information. Do not try to calculate holidays in SQL -- Easter alone requires too much math.
CREATE TABLE Calendar (cal_date DATE NOT NULL PRIMARY KEY, fiscal_year SMALLINT NOT NULL, fiscal_month SMALLINT NOT NULL, week_in_year SMALLINT NOT NULL, -- SQL Server is not ISO standard SMALLINT NOT NULL, -- day_in_year julian_business_day INTEGER NOT NULL, ...);
Since SQL is a database language, we prefer to do look ups and not calculations. They can be optimized while temporal math messes up optimization. A useful idiom is a report period calendar that everyone uses so there is no way to get disagreements in the DML. The report period table gives a name to a range of dates that is common to the entire enterprise.
CREATE TABLE Report_Periods (report_name VARCHAR(30) NOT NULL PRIMARY KEY, report_start_date DATE DEFAULT CURRENT_TIMESTAMP NOT NULL, report_end_date DATE NOT NULL, CONSTRAINT date_ordering CHECK (report_start_date <= report_end_date), etc);
These report periods can overlap or have gaps. I like the MySQL convention of using double zeroes for months and years, That is 'yyyy-mm-00' for a month within a year and 'yyyy-00-00' for the whole year. The advantage is that it will sort with the ISO-8601 data format required by Standard SQL.
Weeks within Year:
I would build a calendar table with a ISO-8601 week-within-year format column in addition to the usual Common Era date. This format is 'yyyyWww-d' where yyyy is the year, W is as separator token, ww is (01-53) week number and d is (1-7) day of the week.
You input any calendar date, find the week-within-year column and return the dates that match on a LIKE predicate. WHERE sale_day LIKE '2012W26-[67]'
There are several websites with calendars you can cut & paste, but you can start your search with: http://www.calendar-365.com/week-number.html
Books in Celko Series for Morgan-Kaufmann Publishing Analytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQL SQL for Smarties SQL Programming Style SQL Puzzles and Answers Thinking in Sets Trees and Hierarchies in SQL
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, October 26, 2012 3:17 PM
Points: 22,
Visits: 591
|
|
| 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?
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
I suppose at this point it's academic: what would a possible query look like?
SELECT cal_date FROM Calendar WHERE cal_date BETWEEN @in_start_date AND @in_end_date
Books in Celko Series for Morgan-Kaufmann Publishing Analytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQL SQL for Smarties SQL Programming Style SQL Puzzles and Answers Thinking in Sets Trees and Hierarchies in SQL
|
|
|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Friday, October 26, 2012 3:17 PM
Points: 22,
Visits: 591
|
|
| how about a query without setting up all that extra stuff... using only my original ddl and sample data.
|
|
|
|
|
SSC-Insane
         
Group: General Forum Members
Last Login: Today @ 12:22 AM
Points: 21,588,
Visits: 27,384
|
|
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)
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, January 15, 2013 11:11 AM
Points: 1,945,
Visits: 2,782
|
|
how about a query without setting up all that extra stuff... using only my original DDL and sample data.
Your weak DDL will lead to self-joins and complicated queries while destroying data integrity in the DML. Again, SQL is a data language and not a procedural or computational one. The more you can put into the DDL, then you know that it is right, it is consistent and that everyone who follows you will be protected.
Lynn gave you an answer that you can use to avoid being a good SQL programmer, if that is what you really want to do. Start thinking in sets and logic, not records and procedural code.
Books in Celko Series for Morgan-Kaufmann Publishing Analytics and OLAP in SQL Data and Databases: Concepts in Practice Data, Measurements and Standards in SQL SQL for Smarties SQL Programming Style SQL Puzzles and Answers Thinking in Sets Trees and Hierarchies in SQL
|
|
|
|