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 |
|
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-03-26 : 11:49:51
|
Hi Folks,I have a date related query that I need help withI have the following table,ref, salary, saldate10, 12000, 1-Jan-0810, 12500,1-Sep-0810, 13000, 1-Jan-0912, 11000, 1-jan-0813, 27000, 1-oct-0813, 28000, 1-jan-09What I want is to use a parameter such as start date and end date (based on the saldate field) to show all salary changes in this period.ie, select salary from salarytable where saldate >= '1-Aug-08' and saldate <= '1-Jan-09' This will return 2 rows.. However, this does not show that ref 10 had a salary of 12000 on the 1-aug-08 (the criteria).IS this possible?thanksCraig. |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-03-26 : 13:15:45
|
Here is one way to show the salary for a given Ref As-Of a certain date:declare @t table (ref int, salary int, saldate datetime)insert @t select 10, 12000, '1-Jan-08' union allselect 10, 12500, '1-Sep-08' union allselect 10, 13000, '1-Jan-09'declare @SalareAsOfDate datetimeset @SalareAsOfDate = '2008-08-01';with cteas(select ref ,salary ,saldate ,row_number() over (partition by ref order by saldate) rnfrom @t)select curr.ref ,[SalaryAsOf] = @SalareAsOfDate ,curr.salaryfrom cte currleft outer join cte nxt on nxt.ref = curr.ref and nxt.rn-1 = curr.rnwhere curr.salDate <= @SalareAsOfDateand isNull(nxt.salDate, '2900-01-01') > @SalareAsOfDateOUTPUT:ref SalaryAsOf salary----------- ----------------------- -----------10 2008-08-01 00:00:00.000 12000 EDIT:correctedBe One with the OptimizerTG |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2009-03-26 : 13:38:38
|
or maybe this is a little easier:select top 1 *from @twhere saldate < @SalareAsOfDateorder by saldate Be One with the OptimizerTG |
 |
|
|
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-03-27 : 04:30:05
|
| Thanks for the replys guys, but I dont think you are picking me up correctly.. I need it to show all 3 fields as the startdate criteria is 1-Aug-08, which means that the employee would be on 12000 at that time.. I need the query to show this.thanks. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-03-27 : 04:39:48
|
[code]DECLARE @Sample TABLE ( ref INT, salary MONEY, saldate DATETIME )INSERT @SampleSELECT 10, 12000, '1-Jan-08' UNION ALLSELECT 10, 12500, '1-Sep-08' UNION ALLSELECT 10, 13000, '1-Jan-09' UNION ALLSELECT 12, 11000, '1-jan-08' UNION ALLSELECT 13, 27000, '1-oct-08' UNION ALLSELECT 13, 28000, '1-jan-09'declare @SalaryAsOfDate datetimeset @SalaryAsOfDate = '2008-08-01'SELECT ref, salary, saldateFROM ( SELECT ref, salary, saldate, row_number() over (partition by ref order by saldate desc) as recid FROM @Sample WHERE saldate <= @SalaryAsOfDate ) AS dWHERE recID = 1[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-03-27 : 05:14:15
|
| I've just realised that the db is sql 2000.This function does not work... |
 |
|
|
cjonline
Yak Posting Veteran
55 Posts |
Posted - 2009-03-27 : 10:43:13
|
| Its ok guys, Ive figured it out and got it working. thanks. |
 |
|
|
|
|
|
|
|