Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 4th, 2004, 09:53 AM
dancesport_warr's Avatar
dancesport_warr dancesport_warr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 130 dancesport_warr User rank is Lance Corporal (50 - 100 Reputation Level)dancesport_warr User rank is Lance Corporal (50 - 100 Reputation Level)dancesport_warr User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 14 h 14 m 36 sec
Reputation Power: 5
problem getting first & last employee hired

Hi,

I can't form the appropriate query for this task:

Display the department number and name of all departments and the full name of the employee(s) hired first and last in each department.

The tables Im working with and their specs are as follows:

Table Employees
----------------
EMPLOYEE_ID NOT NULL NUMBER(6)
FIRST_NAME VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(25)
EMAIL NOT NULL VARCHAR2(25)
PHONE_NUMBER VARCHAR2(20)
HIRE_DATE NOT NULL DATE
JOB_ID NOT NULL VARCHAR2(10)
SALARY NUMBER(8,2)
COMMISSION_PCT NUMBER(2,2)
MANAGER_ID NUMBER(6)
DEPARTMENT_ID NUMBER(4)

Table Departments
------------------
DEPARTMENT_ID NOT NULL NUMBER(4)
DEPARTMENT_NAME NOT NULL VARCHAR2(30)
MANAGER_ID NUMBER(6)
LOCATION_ID NUMBER(4)

Your help is greatly appreciated.

Reply With Quote
  #2  
Old November 4th, 2004, 10:15 AM
Sasi242 Sasi242 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 9 Sasi242 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 22 sec
Reputation Power: 0
Hi Warr

Try This

SQL> ED
Wrote file afiedt.buf

1* SELECT DEPTNO,MIN(HIREDATE) FIRST,MAX(HIREDATE) LAST FROM EMP GROUP BY DEPTNO
SQL> /

DEPTNO FIRST LAST
---------- --------- ---------
10 09-JUN-81 23-JAN-82
20 17-DEC-80 23-MAY-87
30 20-FEB-81 03-DEC-81
This Query I Wrote On the default emp,dept table.make necessary changes...

Happy Coding

sasi242@yahoo.com

Reply With Quote
  #3  
Old November 4th, 2004, 10:21 AM
dancesport_warr's Avatar
dancesport_warr dancesport_warr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 130 dancesport_warr User rank is Lance Corporal (50 - 100 Reputation Level)dancesport_warr User rank is Lance Corporal (50 - 100 Reputation Level)dancesport_warr User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 14 h 14 m 36 sec
Reputation Power: 5
well ummm... I NEED THE NAMES of the employees hired first and last in each department

Also, I need the Department name which is found in the departments table

Reply With Quote
  #4  
Old November 4th, 2004, 11:50 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,775 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 16 h 2 m 7 sec
Reputation Power: 37
Code:
select d.department_Id,
       d.department_name,
       ef.first_Name,
       ef.last_name,
       el.first_name,
       ef.first_name
  from departments D ,
       employees ef,
       employees el
 where d.department_id = ef.department_id
   and d.department_id = el.department_id
   and ef.hire_date in (
      select min(hire_date) from employees e
       where e.department_id = ef.department_id)
   and el.hire_date in (
      select max(hire_date) from employees e
       where e.department_id = el.department_id)

Reply With Quote
  #5  
Old November 4th, 2004, 12:25 PM
dancesport_warr's Avatar
dancesport_warr dancesport_warr is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 130 dancesport_warr User rank is Lance Corporal (50 - 100 Reputation Level)dancesport_warr User rank is Lance Corporal (50 - 100 Reputation Level)dancesport_warr User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Day 14 h 14 m 36 sec
Reputation Power: 5
This does not work. For some departments I get more rows then there are employees in that department.

Also, this is old syntax (8i). I am required to write queries in the new iSQL Plus 1999 syntax!

Thanks for trying though

Reply With Quote
  #6  
Old November 4th, 2004, 01:06 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,775 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 16 h 2 m 7 sec
Reputation Power: 37
I tested it with some sample data and it gave the expected result. Could you show your sample data (in the form of insert statements).

I don't understand why you are not allowed to use that syntax. It is still standard SQL.

Reply With Quote
  #7  
Old November 4th, 2004, 01:06 PM
shafique shafique is offline
Senior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Canada
Posts: 305 shafique User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 45 m 20 sec
Reputation Power: 5
Code:
SELECT empno, ename, hiredate, deptno
FROM emp
WHERE hiredate IN (SELECT max(hiredate) FROM emp GROUP BY deptno 
                   UNION
                   SELECT min(hiredate) FROM emp GROUP BY deptno)
                          
ORDER BY deptno

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > problem getting first & last employee hired


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway