Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-08-23 : 01:30:02
|
| First of all, I need to preface that I know there are much better ways to do this. I know it would be preferable to either set up a calendar table, stored procedure, UDF or even a while loop to do this. However,we have a business need to set this up in a select statement. I'm not able to put any stored procedures, UDF's, views or tables into the database for this application. We're really only limited to select statements.With that, I have the following query. I would like to add a column that shows a 14 day period starting with the first day (@firstday). I will also need to set a period year for each period (not necessarily the same year of the date...ie end of December and Beginning of January have crossover some years). I also need to set a period number for each period.Here is the current query that I put together for this:[CODE]DECLARE @FirstDay DATESET @FirstDay='2001-01-08'SELECT DATEADD(DD, A.NUMBER, @FirstDay) AS SDate, SeqFROM(SELECT (v1.number * 2048) + v2.number AS NUMBER, ROW_NUMBER() OVER (ORDER BY (v1.number * 2048) + v2.number) AS SeqFROM master.dbo.spt_values v1 CROSS JOIN master.dbo.spt_values v2WHERE v1.type = 'P' AND v2.type = 'P' AND ((v1.number * 2048) + v2.number) BETWEEN 0 AND 36500) AWHERE DATEADD(DD, A.NUMBER, @FirstDay)>='2009-12-28' AND DATEADD(DD, A.NUMBER, @FirstDay)<='2010-01-10'ORDER BY 1[/CODE]Desired Results once the 4 additional columns are added:[CODE]Date PayYear PayPeriod DayofPeriod WeekofPeriod2009-12-28 2010 1 1 12009-12-29 2010 1 2 12009-12-30 2010 1 3 12009-12-31 2010 1 4 12010-01-01 2010 1 5 12010-01-02 2010 1 6 12010-01-03 2010 1 7 12010-01-04 2010 1 8 22010-01-05 2010 1 9 22010-01-06 2010 1 10 22010-01-07 2010 1 11 22010-01-08 2010 1 12 22010-01-09 2010 1 13 22010-01-10 2010 1 14 2[/code]**Note: The Seq column isn't needed in the final result setThanks! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-23 : 01:33:25
|
| how do you determine when a period should start and end as you've specified there are cross overs . is there any predefined logic for defining periods?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-08-23 : 01:41:21
|
| The first date: @firstday is the start of the sequence. From there, I want to have consecutive numbering that partitions at "14"So for example...2001-01-08 = 12001-01-09 = 22001-01-10 = 32001-01-11 = 42001-01-12 = 52001-01-13 = 62001-01-14 = 72001-01-15 = 82001-01-16 = 92001-01-17 = 102001-01-18 = 112001-01-19 = 122001-01-20 = 132001-01-21 = 142001-01-22 = 1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-23 : 02:11:08
|
| [code]DECLARE @FirstDay DATESET @FirstDay='2001-01-08';With CTE(DateVal)AS(SELECT @FirstDayUNION ALLSELECT DATEADD(dd,1,DateVal)FROM CTEWHERE DATEADD(dd,1,DateVal)<= DATEADD(yy,1,@firstDay))SELECT DateVal,(sq-1) % 14 + 1 AS PeriodNo,(((sq-1) % 14 + 1)-1) / 7 +1 AS PeriodWeekFROM(SELECT DateVal,ROW_NUMBER() OVER (ORDER BY DateVal) AS SqFROM CTE)tOPTION (MAXRECURSION 0)output-------------------------------------------------DateVal PeriodNo PeriodWeek2001-01-08 1 12001-01-09 2 12001-01-10 3 12001-01-11 4 12001-01-12 5 12001-01-13 6 12001-01-14 7 12001-01-15 8 22001-01-16 9 22001-01-17 10 22001-01-18 11 22001-01-19 12 22001-01-20 13 22001-01-21 14 22001-01-22 1 12001-01-23 2 12001-01-24 3 12001-01-25 4 12001-01-26 5 12001-01-27 6 12001-01-28 7 12001-01-29 8 22001-01-30 9 22001-01-31 10 22001-02-01 11 22001-02-02 12 22001-02-03 13 22001-02-04 14 22001-02-05 1 12001-02-06 2 12001-02-07 3 12001-02-08 4 12001-02-09 5 12001-02-10 6 12001-02-11 7 12001-02-12 8 22001-02-13 9 22001-02-14 10 22001-02-15 11 22001-02-16 12 22001-02-17 13 22001-02-18 14 22001-02-19 1 12001-02-20 2 12001-02-21 3 12001-02-22 4 12001-02-23 5 12001-02-24 6 12001-02-25 7 12001-02-26 8 22001-02-27 9 22001-02-28 10 22001-03-01 11 22001-03-02 12 22001-03-03 13 22001-03-04 14 22001-03-05 1 12001-03-06 2 12001-03-07 3 12001-03-08 4 12001-03-09 5 12001-03-10 6 12001-03-11 7 12001-03-12 8 22001-03-13 9 22001-03-14 10 22001-03-15 11 22001-03-16 12 22001-03-17 13 22001-03-18 14 22001-03-19 1 12001-03-20 2 12001-03-21 3 12001-03-22 4 12001-03-23 5 12001-03-24 6 12001-03-25 7 12001-03-26 8 22001-03-27 9 22001-03-28 10 22001-03-29 11 22001-03-30 12 22001-03-31 13 22001-04-01 14 22001-04-02 1 12001-04-03 2 12001-04-04 3 12001-04-05 4 12001-04-06 5 12001-04-07 6 12001-04-08 7 12001-04-09 8 22001-04-10 9 22001-04-11 10 22001-04-12 11 22001-04-13 12 22001-04-14 13 22001-04-15 14 22001-04-16 1 12001-04-17 2 12001-04-18 3 12001-04-19 4 12001-04-20 5 12001-04-21 6 12001-04-22 7 12001-04-23 8 22001-04-24 9 22001-04-25 10 22001-04-26 11 22001-04-27 12 22001-04-28 13 22001-04-29 14 22001-04-30 1 12001-05-01 2 12001-05-02 3 12001-05-03 4 12001-05-04 5 12001-05-05 6 12001-05-06 7 12001-05-07 8 22001-05-08 9 22001-05-09 10 22001-05-10 11 22001-05-11 12 22001-05-12 13 22001-05-13 14 22001-05-14 1 12001-05-15 2 12001-05-16 3 12001-05-17 4 12001-05-18 5 12001-05-19 6 12001-05-20 7 12001-05-21 8 22001-05-22 9 22001-05-23 10 22001-05-24 11 22001-05-25 12 22001-05-26 13 22001-05-27 14 22001-05-28 1 12001-05-29 2 12001-05-30 3 12001-05-31 4 12001-06-01 5 12001-06-02 6 12001-06-03 7 12001-06-04 8 22001-06-05 9 22001-06-06 10 22001-06-07 11 22001-06-08 12 22001-06-09 13 22001-06-10 14 22001-06-11 1 12001-06-12 2 12001-06-13 3 12001-06-14 4 12001-06-15 5 12001-06-16 6 12001-06-17 7 12001-06-18 8 22001-06-19 9 22001-06-20 10 22001-06-21 11 22001-06-22 12 22001-06-23 13 22001-06-24 14 22001-06-25 1 12001-06-26 2 12001-06-27 3 12001-06-28 4 12001-06-29 5 12001-06-30 6 12001-07-01 7 12001-07-02 8 22001-07-03 9 22001-07-04 10 22001-07-05 11 22001-07-06 12 22001-07-07 13 22001-07-08 14 22001-07-09 1 12001-07-10 2 12001-07-11 3 12001-07-12 4 12001-07-13 5 12001-07-14 6 12001-07-15 7 12001-07-16 8 22001-07-17 9 22001-07-18 10 22001-07-19 11 22001-07-20 12 22001-07-21 13 22001-07-22 14 22001-07-23 1 12001-07-24 2 12001-07-25 3 12001-07-26 4 12001-07-27 5 12001-07-28 6 12001-07-29 7 12001-07-30 8 22001-07-31 9 22001-08-01 10 22001-08-02 11 22001-08-03 12 22001-08-04 13 22001-08-05 14 22001-08-06 1 12001-08-07 2 12001-08-08 3 12001-08-09 4 12001-08-10 5 12001-08-11 6 12001-08-12 7 12001-08-13 8 22001-08-14 9 22001-08-15 10 22001-08-16 11 22001-08-17 12 22001-08-18 13 22001-08-19 14 22001-08-20 1 12001-08-21 2 12001-08-22 3 12001-08-23 4 12001-08-24 5 12001-08-25 6 12001-08-26 7 12001-08-27 8 22001-08-28 9 22001-08-29 10 22001-08-30 11 22001-08-31 12 22001-09-01 13 22001-09-02 14 22001-09-03 1 12001-09-04 2 12001-09-05 3 12001-09-06 4 12001-09-07 5 12001-09-08 6 12001-09-09 7 12001-09-10 8 22001-09-11 9 22001-09-12 10 22001-09-13 11 22001-09-14 12 22001-09-15 13 22001-09-16 14 22001-09-17 1 12001-09-18 2 12001-09-19 3 12001-09-20 4 12001-09-21 5 12001-09-22 6 12001-09-23 7 12001-09-24 8 22001-09-25 9 22001-09-26 10 22001-09-27 11 22001-09-28 12 22001-09-29 13 22001-09-30 14 22001-10-01 1 12001-10-02 2 12001-10-03 3 12001-10-04 4 12001-10-05 5 12001-10-06 6 12001-10-07 7 12001-10-08 8 22001-10-09 9 22001-10-10 10 22001-10-11 11 22001-10-12 12 22001-10-13 13 22001-10-14 14 22001-10-15 1 12001-10-16 2 12001-10-17 3 12001-10-18 4 12001-10-19 5 12001-10-20 6 12001-10-21 7 12001-10-22 8 22001-10-23 9 22001-10-24 10 22001-10-25 11 22001-10-26 12 22001-10-27 13 22001-10-28 14 22001-10-29 1 12001-10-30 2 12001-10-31 3 12001-11-01 4 12001-11-02 5 12001-11-03 6 12001-11-04 7 12001-11-05 8 22001-11-06 9 22001-11-07 10 22001-11-08 11 22001-11-09 12 22001-11-10 13 22001-11-11 14 22001-11-12 1 12001-11-13 2 12001-11-14 3 12001-11-15 4 12001-11-16 5 12001-11-17 6 12001-11-18 7 12001-11-19 8 22001-11-20 9 22001-11-21 10 22001-11-22 11 22001-11-23 12 22001-11-24 13 22001-11-25 14 22001-11-26 1 12001-11-27 2 12001-11-28 3 12001-11-29 4 12001-11-30 5 12001-12-01 6 12001-12-02 7 12001-12-03 8 22001-12-04 9 22001-12-05 10 22001-12-06 11 22001-12-07 12 22001-12-08 13 22001-12-09 14 22001-12-10 1 12001-12-11 2 12001-12-12 3 12001-12-13 4 12001-12-14 5 12001-12-15 6 12001-12-16 7 12001-12-17 8 22001-12-18 9 22001-12-19 10 22001-12-20 11 22001-12-21 12 22001-12-22 13 22001-12-23 14 22001-12-24 1 12001-12-25 2 12001-12-26 3 12001-12-27 4 12001-12-28 5 12001-12-29 6 12001-12-30 7 12001-12-31 8 22002-01-01 9 22002-01-02 10 22002-01-03 11 22002-01-04 12 22002-01-05 13 22002-01-06 14 22002-01-07 1 12002-01-08 2 1[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-23 : 02:11:59
|
| I've shown it for a year and you can repeat it similarly for any number of years------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-08-23 : 02:13:31
|
| That's wicked Visakh...but I should have said before, I can't use CTE's in this environment either. Do you think it could be achieved using derived tables? |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-08-23 : 02:18:20
|
Actually, unless I'm missing something, it seems to be calculating properly when I just add your "sq" columns to my query like so:DECLARE @FirstDay DATESET @FirstDay='2001-01-08'SELECT DATEADD(DD, A.NUMBER, @FirstDay) AS SDate, seq, (seq-1) % 14 + 1, (((seq-1) % 14 + 1)-1) / 7 +1FROM(SELECT (v1.number * 2048) + v2.number AS NUMBER, ROW_NUMBER() OVER (ORDER BY (v1.number * 2048) + v2.number) AS SeqFROM master.dbo.spt_values v1 CROSS JOIN master.dbo.spt_values v2WHERE v1.type = 'P' AND v2.type = 'P' AND ((v1.number * 2048) + v2.number) BETWEEN 0 AND 36500) AWHERE DATEADD(DD, A.NUMBER, @FirstDay)>='2009-12-28' and DATEADD(DD, A.NUMBER, @FirstDay)<='2010-01-10'ORDER BY 1 |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-08-23 : 02:49:49
|
| Vis, it looks like it's working like I noted before, but I'm not really following the logic for the 2 columns:(seq-1) % 14 + 1 AS DayOfPeriod, (((seq-1) % 14 + 1)-1) / 7 +1 AS WeekOfPeriodIt's obvious to me that I'm not understanding it because when I try to apply a similar approach to the PayYear and the PayPeriod number, I get nothing. So, like I mentioned earlier, the pay year is dependent on when the pay periods start for the year. Since the first pay year was 2001, that's what's set as PayYear 1. The number of pay periods in a year depend on the year. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-23 : 02:57:48
|
| seq is just a sequence number in my case. if you want to replace it with payyear how should it go? should it repeat 1 to 14 for whole year?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
flamblaster
Constraint Violating Yak Guru
384 Posts |
Posted - 2011-08-23 : 09:30:57
|
| Sorry, I know I'm not explaining this very well. I have a function that accomplishes what I'm trying to do (author-Wolffy from DeveloperBarn a few years ago). It works well and I can follow it. I just can't use it in my current application, so I was trying to achieve the same idea through a select statement. Here's the function. Hopefully this makes it more clear:[CODE]create table test.dbo.PayPeriods (Date_Id int Primary Key not null, Date_Index datetime not null, Pay_Year smallint not null, Pay_Period tinyint not null, Day_Of_Period tinyint not null)DECLARE @lastDay as DateTime , @firstDay as DateTime , @payYear as integer , @payPeriod as TinyInt , @dayOfPeriod as TinyInt , @looper as integer , @thisDay as DateTime ;BEGIN Set @firstDay = CAST('2001-01-08' as DateTime); Set @lastDay = DATEADD(d, 364, @firstDay) Set @looper = 1; Set @payYear = 2010; While @looper <= 20 BEGIN Set @payPeriod = 1; Set @dayOfPeriod = 1; Set @thisDay = @firstDay While @thisDay < @lastDay BEGIN INSERT INTO test.dbo.PayPeriods (DATE_ID, DATE_INDEX, PAY_YEAR,PAY_PERIOD, DAY_OF_PERIOD) VALUES(cast(convert(char(10), @thisDay,112) as int), @thisDay, @payYear, @payPeriod, @dayOfPeriod); Set @dayOfPeriod = @dayOfPeriod + 1; If @dayOfPeriod >14 BEGIN Set @dayOfPeriod = 1; Set @payPeriod = @payPeriod + 1 END Set @thisDay = DATEADD(d, 1, @thisDay); END Set @payYear = @payYear + 1 Set @firstDay = @lastDay Set @lastDay = DATEADD(d, 364, @firstDay) Set @looper = @looper + 1 END END;select * from Test.dbo.PayPeriods[/CODE] |
 |
|
|
|
|
|
|
|