SunQuest
           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:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old April 13th, 2004, 07:47 PM
YuLing YuLing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 44 YuLing User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via ICQ to YuLing
Question Find records that have the most current date??

Hi

I need some help again. I have two tables

table_1
--Emp_id--|--Sup_id--|Sup_name|--Date--|
-------------------------------------------------
--00001--|--00005 --|---ABCD--|01-MARCH-2004
--00002--|--00006 --|---BCDE--|02-MARCH-2004
--00003--|--00007 --|---CDEF--|03-MARCH-2004
--00001--|--00008 --|---DEFG--|04-APRIL-2004
--00003--|--00009 --|---EFGH--|05-APRIL-2004

table_2
--Emp_id--|Emp_name|
--------------------------------
--00001--|--QWER--|
--00002--|--ASDF--|
--00003--|--ZXCV--|
--00004--|--POIU--|

table_1 contain records on employee and the supervisor they are under at a certain date.
As some employee(00001 & 00003) have a different supervisor from different date, I'll like to extract from table_1 the record of each employee in the table that only contain the supervisor info on the most recent date.
And from table_2, i'll like to extract the employee's name.
These records extracted from both the tables would the be put into a new table,table_3

Example:
For employee 00001, only extract record that have the most recent date which is 04-APRIL-2004 and not on 01-MARCH-2004

table_3
Emp_id|Emp_name|Sup_id|Sup_name|Date|
------------------------------------------------
00001 |--QWER--|00008 |--DEFG---|04-APRIL-2004
00002 |--ASDF-- |00006 |--BCDE---|02-MARCH-2004
00003 |--ZXCV-- |00009 |--EFGH---|05-APRIL-2004

Confusing???
How to write an SQL statement to perform this??
Course work on Oracle is really killing me ...hope someone can help me out.
Thanks

regard,
YuLing

Reply With Quote
  #2  
Old April 13th, 2004, 09:05 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
CREATE TABLE table3 AS SELECT
a.emp_id,b.emp_name,a.supp_id,a.supp_name,a.date
FROM table1 a, table2 b
WHERE (a.emp_id,a.date) = (SELECT emp_id, max(date) from table1 GROUP BY emp_id)
AND a.emp_id = b.emp_id

Reply With Quote
  #3  
Old April 13th, 2004, 10:07 PM
YuLing YuLing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 44 YuLing User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via ICQ to YuLing
i try out the sql but i got this error msg

WHERE (a.emp_id,a.date) = (SELECT emp_id, max(date) from table1 GROUP BY emp_id)
*
ERROR at line 5:
ORA-01427: single-row subquery returns more than one row

then on further checking i realize that some of the record in table_1 have the same date for their max date.

Emp_no|Date
-------------------------
00001 |01-March-2004
00001 |01-APRIL-2004
00001 |01-APRIL-2004

If it is this case,can i still know which date is the most currently updated?

Thanks

regards,
YuLing

Reply With Quote
  #4  
Old April 13th, 2004, 10:20 PM
YuLing YuLing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 44 YuLing User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via ICQ to YuLing
If the most current date cant be know when both record have the same date, then is it possible to extract out both of the record that contain the most recent date??

Example

Emp_no|Emp_name|Sup_no|Sup_name|Date
-00001-|--ABCD--|-0005-|--QWER--|01-APRIL-2004
-00001-|--ABCD--|-0006-|--ASDF --|01-APRIL-2004
.
.
.

Thanks

Reply With Quote
  #5  
Old April 14th, 2004, 07:34 AM
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
Just make the following modification in your where clause.

WHERE (a.emp_id,a.date) IN (SELECT emp_id, max(date) from table1 GROUP BY emp_id)

Reply With Quote
  #6  
Old April 14th, 2004, 08:27 PM
YuLing YuLing is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 44 YuLing User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via ICQ to YuLing
Red face one more question

Thanks for all ur help shafique

but so sorry to bother u again, cos there is one more question i would like to ask...

Is it possible to only extract the last row from the many rows of the latest date??

Example:

table_1
--Emp_id--|--Sup_id--|Sup_name|--Date--|
-------------------------------------------------
--00001--|--00005 --|---ABCD--|01-MARCH-2004
--00001--|--00006 --|---BCDE--|01-APRIL-2004
--00001--|--00007 --|---CDEF--|01-APRIL-2004
--00001--|--00008 --|---DEFG--|01-APRIL-2004

table_2
--Emp_id--|Emp_name|
--------------------------------
--00001--|--QWER--|


From the table_1 and table_2, produce the following table_3
table_3
Emp_id |Emp_name| Sup_id |Sup_name|Date
-00001-|--QWER--|-00008-|--DEFG--|01-APRIL-2004

Thank you.

Last edited by YuLing : April 15th, 2004 at 01:25 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Find records that have the most current date??


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