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 |
|
amsqlguy
Yak Posting Veteran
89 Posts |
Posted - 2008-07-29 : 15:20:48
|
| GuysI have salary records for all employees, but based on latest moddate field value I need to mark down the current salary of the employee.EMPID SALARY MODDATE CURRENT1 100 04/08/2007 1 100 04/08/2008 Y2 200 04/08/20062 200 04/08/2005 Y2 200 04/08/2004 Is there any way to accomplish this?Any suggestions and inputs would helpThanks |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-07-29 : 15:31:32
|
| Do you mean something like this?;with my_cte as ( selectrow_number() over (partition by empid,moddate order by empid,moddate desc) as rownum,empid,salary,moddate from your_table)selectempid,salary,moddate,case when rownum = 1 then 'Y' else ''end as currentfrom my_cteGreetingsWebfredThere are 10 types of people in the world: Those who understand binary, and those who don't... |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-07-29 : 15:35:42
|
| Are you saying you want to maintain this table with a [current] flag as modifications to salary are made? So whenever a row is added, you need to find the previous current row, set the flag to null then add the new row with current set to 'y'? Have you considered just returning the most recent data by empid instead maintaining a [current] flag?Be One with the OptimizerTG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-30 : 00:30:26
|
quote: Originally posted by amsqlguy GuysI have salary records for all employees, but based on latest moddate field value I need to mark down the current salary of the employee.EMPID SALARY MODDATE CURRENT1 100 04/08/2007 1 100 04/08/2008 Y2 200 04/08/20062 200 04/08/2005 Y2 200 04/08/2004 Is there any way to accomplish this?Any suggestions and inputs would helpThanks
Is this what you want?SELECT t.*FROM YourTable tINNER JOIN (SELECT EMPID,MAX(MODDATE) AS MaxDate FROM YourTable GROUP BY EMPID)tmpON t.EMPID=tmp.EMPIDAND t.MODDATE=tmp.MaxDate |
 |
|
|
|
|
|
|
|