Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
7/18/2012 7:56:20 AM EDT




My SQL-fu is a bit rusty.



I have the following table



Employee#, Date,(other fields)

1,  1/1/2000,

1,  1/2/2000,

1,  1/3/2000,



2,  1/1/2000,

2,  1/3/2000,

2,  1/4/2000,





I want to select the employee # and the most recent date for that employee. The results should look something like this



1, 1/3/2000,(other fields)

2, 1/4/2000,(other fields)





I don't think Select Distinct will work here.

7/18/2012 8:18:02 AM EDT
[#1]
SELECT EmployeId,MAX(DateFieldName) FROM TableName GROUP BY EmployeId;
7/18/2012 8:18:47 AM EDT
[#2]
select
E.employee_id,
max(A.activity_date) as most_recent_activity_date
from
employees E
left join activities A
on E.employee_id = A.activity_id
where
....
group by
E.employee_id
order by
E.employee_id


Code tag doesn't allow indenting.  Weird.
7/18/2012 8:28:23 PM EDT
[#3]
Assuming you want one row for each employee and that row contains the "other fields" for the one record that has the max date for that employee.
You did not say what db system you are using.  The following should get you close to what you want using MS SQL Server.

select a.*
from employee a
inner join (
select b.employeeID
, max(b.empdate) as empDate
from employee b
group by b.employeeID
) c
on c.employeeID = a.employeeID
and c.empDate = a.empdate

You could also use Row_number() and "partition over".
7/19/2012 1:00:35 PM EDT
[#4]


Thanks all.



I haven't had time to test this yet(other projects in the way) but the rust is starting to come off.



BTW, this is a MS SQL database.
7/23/2012 5:36:45 AM EDT
[#5]
Quoted:
SELECT EmployeId,MAX(DateFieldName) FROM TableName GROUP BY EmployeId;


This works.  I independently came up with something similar:


ruertar=# CREATE TABLE employee ( id integer, infraction date );
CREATE TABLE
ruertar=# SELECT * FROM employee;
id | infraction
––––+––––––––––––
 1 | 1975-05-11
 1 | 1935-05-11
 1 | 1985-05-11
 1 | 1985-03-11
 1 | 1985-03-14
 2 | 1999-01-14
 2 | 2009-01-14
(7 rows)

ruertar=# SELECT id, max(infraction) FROM employee GROUP BY id;
id |    max    
––––+––––––––––––
 1 | 1985-05-11
 2 | 2009-01-14
(2 rows)


Works in PostgreSQL.

PS, Why does ar15.com reformat text in [pre] and
blocks?
7/23/2012 7:44:51 AM EDT
[#6]
Quoted:
Quoted:
SELECT EmployeId,MAX(DateFieldName) FROM TableName GROUP BY EmployeId;


This works.  I independently came up with something similar:

ruertar=# CREATE TABLE employee ( id integer, infraction date );CREATE TABLEruertar=# SELECT * FROM employee; id | infraction ––––+––––––––––––  1 | 1975-05-11  1 | 1935-05-11  1 | 1985-05-11  1 | 1985-03-11  1 | 1985-03-14  2 | 1999-01-14  2 | 2009-01-14(7 rows)ruertar=# SELECT id, max(infraction) FROM employee GROUP BY id; id |    max     ––––+––––––––––––  1 | 1985-05-11  2 | 2009-01-14(2 rows)


Works in PostgreSQL.

PS, Why does ar15.com reformat text in [pre] and
blocks?


But you will have to take the results from that query and do an inner join back against the first table since group by won't allow you to put in any other fields.
7/23/2012 9:11:24 AM EDT
[#7]
Quoted:
But you will have to take the results from that query and do an inner join back against the first table since group by won't allow you to put in any other fields.


Yes –– this is true.  I assume the OP would take any steps to utilize the results of this query (like joining).

Also, great user name. :^)  I did Sun/SPARC development until two years ago.
7/24/2012 8:14:29 AM EDT
[#8]
Quoted:
Quoted:
But you will have to take the results from that query and do an inner join back against the first table since group by won't allow you to put in any other fields.


Yes –– this is true.  I assume the OP would take any steps to utilize the results of this query (like joining).

Also, great user name. :^)  I did Sun/SPARC development until two years ago.


I do love how few people know what it is.  Funniest thing is my wife was trying to figure out a username somewhere so now she is c0t0d0s2