| Author |
Topic |
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-12-10 : 06:41:25
|
| I have the following:DECLARE @StartDate DATETIMEDELCARE @EndDate DATETIMEDECLARE @Levels INTSET @StartDate = '20081128'SET @EndDate = CURRENT_TIMESTAMPSET @Levels = 5-- Desired result set -------Date Level-----------------20081128 120081129 220081130 320081201 420081202 520081203 120081204 220081205 320081206 420081207 520081208 120081209 220081210 3How can I do this? ThanksHearty head pats |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-10 : 06:47:39
|
SELECT Date, (ROW_NUMBER() OVER (ORDER BY Date) - 1) % 5 + 1 AS LevelFROM Table1 E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-12-10 : 06:50:03
|
| Thankyou. Works a treat!Hearty head pats |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-12-10 : 07:43:15
|
| I have another twist. What if I wanted to specify the start number? Therefore, although the date is '20081201', the level for that date is not automatically 1?See results below:DECLARE @StartDate DATETIMEDELCARE @EndDate DATETIMEDECLARE @Levels INTSET @StartDate = '20081201'SET @EndDate = CURRENT_TIMESTAMPSET @Levels = 5Date Level-----------------20081201 420081202 520081203 120081204 220081205 320081206 420081207 520081208 120081209 220081210 3Hearty head pats |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-10 : 09:17:41
|
[code]DECLARE @Sample TABLE ( dt DATETIME )INSERT @SampleSELECT '20081201' UNION ALLSELECT '20081202' UNION ALLSELECT '20081203' UNION ALLSELECT '20081204' UNION ALLSELECT '20081205' UNION ALLSELECT '20081206' UNION ALLSELECT '20081207' UNION ALLSELECT '20081208' UNION ALLSELECT '20081209' UNION ALLSELECT '20081210'DECLARE @StartValue INT, @Levels INTSELECT @StartValue = 4, @Levels = 5SELECT dt, (ROW_NUMBER() OVER (ORDER BY dt) + @StartValue - 2) % @Levels + 1 AS [Level] -- minus one for row_number and minus one for @startvalueFROM @Sample[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-12-10 : 09:45:24
|
Thanks Peso! You're a GENIUS!!!!! Hearty head pats |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-10 : 09:47:19
|
Everything is simple when you know your math. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-12-10 : 09:56:04
|
Thats probably where I go wrong. I more art than maths! Ah well, thank god for you guys to do the hard bits for us Hearty head pats |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-12-10 : 11:40:43
|
| Another twist.Currently, I'm getting (if I add an ORDER DESC clause at the end):10 Dec 2008 5 09 Dec 2008 4 08 Dec 2008 3 07 Dec 2008 2 06 Dec 2008 1 05 Dec 2008 5 04 Dec 2008 403 Dec 2008 302 Dec 2008 201 Dec 2008 130 Nov 2008 529 Nov 2008 428 Nov 2008 3But what I want is:10 Dec 2008 309 Dec 2008 208 Dec 2008 107 Dec 2008 506 Dec 2008 405 Dec 2008 304 Dec 2008 203 Dec 2008 102 Dec 2008 501 Dec 2008 430 Nov 2008 329 Nov 2008 228 Nov 2008 1Any more great advice?SoHearty head pats |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-10 : 12:03:24
|
Begin with ORDER BY dt DESC in the ROW_NUMBER() function... E 12°55'05.63"N 56°04'39.26" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-10 : 12:05:10
|
| [code]SELECT dt, (ROW_NUMBER() OVER (ORDER BY dt)-1) % 5 + 1 AS [Value] FROM @Sample[/code] |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-12-10 : 12:08:56
|
| Now I get this:10 Dec 2008 309 Dec 2008 408 Dec 2008 507 Dec 2008 106 Dec 2008 205 Dec 2008 304 Dec 2008 403 Dec 2008 502 Dec 2008 101 Dec 2008 230 Nov 2008 329 Nov 2008 428 Nov 2008 5Close, but still not quite right? I have a work around, but is it possible using the function you suggested?10 Dec 2008 309 Dec 2008 208 Dec 2008 107 Dec 2008 506 Dec 2008 405 Dec 2008 304 Dec 2008 203 Dec 2008 1Hearty head pats |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-12-10 : 12:27:55
|
I take it back, I don't have a work around Hearty head pats |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-10 : 12:30:36
|
quote: Originally posted by Bex I take it back, I don't have a work around Hearty head pats
No luck with my suggestion? |
 |
|
|
Bex
Aged Yak Warrior
580 Posts |
Posted - 2008-12-11 : 04:28:08
|
| Thanks to you both. I have something that works now!Hearty head pats |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-11 : 10:19:04
|
| ok...cool |
 |
|
|
|