If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Find Two Longest Serving Employees

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-15-04, 18:28
stasik_du stasik_du is offline
Registered User
 
Join Date: Oct 2004
Posts: 12
Find Two Longest Serving Employees

Hello
I have to find two longest serving employees for each job in this table
employees(empname,job,hiredate). how can I do it? thanx.
Reply With Quote
  #2 (permalink)  
Old 11-16-04, 15:01
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
How do you know the termination date for each employee? Without that, I can't figure out how to compute their length of service.

Did the instructor give you a more detailed problem specification, or was this all that they gave you?

-PatP
Reply With Quote
  #3 (permalink)  
Old 11-16-04, 17:47
stasik_du stasik_du is offline
Registered User
 
Join Date: Oct 2004
Posts: 12
I don't need termination date. I need to find out which two current employees are working longer than the others.
Reply With Quote
  #4 (permalink)  
Old 11-17-04, 10:22
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Assuming the employee table is only current employees, try:

SELECT EMPNAME, HIREDATE
FROM EMPLOYEES A
WHERE 2 > (SELECT COUNT(*) FROM EMPLOYEES B
WHERE A.HIREDATE > B.HIREDATE);
Reply With Quote
  #5 (permalink)  
Old 11-17-04, 11:56
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
urquel: for each job
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 11-17-04, 12:12
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
That's simple:

SELECT EMPNAME, JOB, HIREDATE
FROM EMPLOYEES A
WHERE 2 > (SELECT COUNT(*) FROM EMPLOYEES B
WHERE A.HIREDATE > B.HIREDATE AND A.JOB = B.JOB);
Reply With Quote
  #7 (permalink)  
Old 11-17-04, 15:51
stasik_du stasik_du is offline
Registered User
 
Join Date: Oct 2004
Posts: 12
Quote:
Originally Posted by urquel
That's simple:

SELECT EMPNAME, JOB, HIREDATE
FROM EMPLOYEES A
WHERE 2 > (SELECT COUNT(*) FROM EMPLOYEES B
WHERE A.HIREDATE > B.HIREDATE AND A.JOB = B.JOB);
Thanx a lot!!!
That's what I needed!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On