Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
Id    DeptId     EmpId    sal

 1       1         101    100
 2       1         102    100
 3       1         103    300
 4       1         104    300
 5       1         105    300
 6       1         101    500
 7       1         101    400
 8       1         102    300
 9       1         104    250
10       1         103    350


I want output like this
XML
<pre lang="SQL">

 Id    DeptId     EmpId    sal

  5       1         105    300
  7       1         101    400
  8       1         102    300
  9       1         104    250
 10       1         103    350

</pre>


Hi friends, as i have explained in the above table i would like to know that the recent salary of the Employee which means the EmpId is getting repeated here and want distinct EmpId which is not getting repeated based on the Id.
Any help will appreciated. Thanks in advance.
Posted
Updated 5-Aug-14 4:39am
v2
Comments
DiponRoy 22-Aug-14 2:34am    
do you have any payment date or primary key at the table where the "sal" column is ?

Try this:
SQL
select id, deptid, empid, sal from table1 t1 where
id = (select top 1 id from table1 where t1.empid = empid order by id desc)
 
Share this answer
 
This is slightly complicated, since you are going to have to rely on the Id values being in ascending numeric order - if you can, you should consider time-stamping the rows instead to give you a "proper", defined sort order.
This is because SQL server can return records in any order unless you explicitly specify an ORDER BY clause or similar.
So... try this:
SQL
SELECT m.id, m.deptId, m.empId, m.sal
   FROM MyTable m
   LEFT JOIN MyTable n ON m.empId = n.empId AND m.id < n.id
WHERE n.id IS NULL
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900