| Author |
Topic |
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2007-04-12 : 10:47:32
|
| I would like to have a SP in which I pass a month and a year (say 4 and 2007) and then I get the closest entry back looking forward in time.So lets say I have a table with month and year column like so:ID Month Year5 10 20079 3 200810 6 2008And I would pass it 4 and 2007, it would return the record with 10 and 2007, and if I would pass 5 2008 it would return the record with 6 2008I assume this needs quite some looping, so I haven't got a clue how to do this in SQL |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-12 : 10:52:18
|
[code]declare @data table( ID int, [Month] int, [Year] int)insert into @dataselect 5, 10, 2007 union allselect 9, 3, 2008 union allselect 10, 6, 2008declare @month int, @year intselect @month = 4, @year = 2007select top 1 *from @dataorder by abs(datediff(day, dateadd(year, @year - 1900, dateadd(month, @month - 1, 0)), dateadd(year, [Year] - 1900, dateadd(month, [Month] - 1, 0))))[/code] KH |
 |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2007-04-12 : 10:57:46
|
| Wow... looks easier then I thought.Thanks a lot. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-12 : 11:00:38
|
Not that complex actually. As you have your month & year in separate column i have to combine both to form a datetime.Which is done by dateadd(year, @year - 1900, dateadd(month, @month - 1, 0))Next just compare the different in days and take the absolute value and ORDER it. KH |
 |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2007-04-12 : 11:03:18
|
| It doen't really work though, if I enter 4 and 2007 it gives me back a record which has 2 and 2007, so perhaps it looks backward in time...I gives back the closest date though, it's only not the closest date in the futureAnd could you explain the "- 1900" and the "month -1" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-12 : 11:06:06
|
oh you only wanted future date ?select top 1 *from @dataorder by abs(datediff(day, dateadd(year, @year - 1900, dateadd(month, @month - 1, 0)), dateadd(year, [Year] - 1900, dateadd(month, [Month] - 1, 0)))) KH |
 |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2007-04-12 : 11:07:29
|
| Very cool... |
 |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2007-04-12 : 11:10:37
|
| Still returning 2 2007 though.... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-12 : 11:11:14
|
quote: And could you explain the "- 1900" and the "month -1"
Sure. dateadd(year, 2007 - 1900, 0) will add 2007 - 1900 which is 107 years to date 0. Date 0 in SQL is 1900-01-01. So it is adding 107 years to 1900 which will gives you 2007-01-01.Same concept as the month - 1. Month starts from 1 not 0. So the dateadd(month, 4 - 1, 0) will convert to 1900-04-01. KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-12 : 11:13:03
|
quote: Originally posted by trouble2 Still returning 2 2007 though....
declare @data table( ID int, [Month] int, [Year] int)insert into @dataselect 2, 10, 2007 union allselect 6, 10, 2007 union allselect 9, 3, 2008 union allselect 10, 6, 2008declare @month int, @year intselect @month = 4, @year = 2007select top 1 *from @dataorder by datediff(day, dateadd(year, @year - 1900, dateadd(month, @month - 1, 0)), dateadd(year, [Year] - 1900, dateadd(month, [Month] - 1, 0)))ID Month Year ----------- ----------- ----------- 6 10 2007 KH |
 |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2007-04-12 : 11:19:19
|
| What if you do: insert into @dataselect 2, 2, 2007 union allselect 6, 10, 2007 union allselect 9, 3, 2008 union allselect 10, 6, 2008It should return:ID Month Year ----------- ----------- ----------- 6 10 2007 |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-04-12 : 14:39:38
|
You can do this without converting to dates.declare @year intdeclare @month intselect @year = 2007, @month = 4declare @t table ( ID int not null, Month int not null, Year int not null )insert into @tselect id=5, Month=10, Year=2007 union allselect 9, 3, 2008 union allselect 10, 6, 2008select *from @twhere ID = (select ID=min(ID) from @t where (year*100)+ Month = ( select min((year*100)+ Month) from @t where (year*100)+ Month > (@year*100)+ @Month ))Results:ID Month Year ----------- ----------- ----------- 5 10 2007(1 row(s) affected) CODO ERGO SUM |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-12 : 15:19:24
|
| [code]SELECT TOP 1 [Year], [Month]FROM @tWHERE 12 * [Year] + [Month] >= 12 * @Year + @MonthORDER BY [Year], [Month][/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
trouble2
Constraint Violating Yak Guru
267 Posts |
Posted - 2007-04-13 : 05:08:37
|
| Hey Peter, that seems to work great...Can you explain what you did? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-13 : 06:43:56
|
quote: Originally posted by khtan
quote: Originally posted by trouble2 Still returning 2 2007 though....
declare @data table( ID int, [Month] int, [Year] int)insert into @dataselect 2, 10, 2007 union allselect 6, 10, 2007 union allselect 9, 3, 2008 union allselect 10, 6, 2008declare @month int, @year intselect @month = 4, @year = 2007select top 1 *from @dataorder by datediff(day, dateadd(year, @year - 1900, dateadd(month, @month - 1, 0)), dateadd(year, [Year] - 1900, dateadd(month, [Month] - 1, 0)))ID Month Year ----------- ----------- ----------- 6 10 2007 KH
Did you test with these?select @month = 12, @year = 2007 MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-04-13 : 06:45:13
|
quote: Originally posted by Peso
SELECT TOP 1 [Year], [Month]FROM @tWHERE 12 * [Year] + [Month] >= 12 * @Year + @MonthORDER BY [Year], [Month] Peter LarssonHelsingborg, Sweden
Is this correct?SELECT TOP 1 [Year], [Month]FROM @tWHERE N * [Year] + [Month] >= N * @Year + @MonthORDER BY [Year], [Month] Where N>=12MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-13 : 08:21:33
|
| Yes. Any number >= 12 will do.But 12 is the only number usable if you want to calculate differences and additions to dates with month and year only.Peter LarssonHelsingborg, Sweden |
 |
|
|
|
|
|