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:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old April 26th, 2004, 03:44 AM
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
Find new location value??

Hi..

I was wondering whether this can be done or not...

I alter my table by adding a new column to store employee's new location...now i need to populate the column with their new location...which i the part i do not know how to do.

I break this up so it is easier to understand
1) Need to first determine whether employee name are repeated or not in the table

2) If the employee's name occur only once then the new location column value should just be the old location value.

3) But when an employee's name occur more than one time on the table...then those few rows with the same employee name needs to be futher compare on their create date to know the new location value.

Example:
|Name|O_loc|-C_date-|
|ABCD|--A--|1/1/2003|
|BCDE|--A--|1/1/2003|
|ABCD|--B--|1/6/2003|
|ABCD|--C--|1/9/2003|

Result in
|Name|O_loc|-C_date-|N-loc|
|ABCD|--A--|1/1/2003|--C--|
|BCDE|--A--|1/1/2003|--A--|
|ABCD|--B--|1/6/2003|--C--|
|ABCD|--C--|1/9/2003|--C--|

*find the max(C_date) among the 3 rows with employee by the same name, ABCD, then put the O_loc value of that latest row into the 3 other N_loc column value.
this is the part i really dont know whether can be done or not

If this way can not be accomplish, can anyone suggest a better way to do it...the main thing that i really need is to find out the lastest location of an employee...

Thanks


regards,
YuLing

Reply With Quote
  #2  
Old April 26th, 2004, 01:15 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
This might be tough enough query for you. However, try to understand the attached query yourself, if you need explaination, let me know.
Attached Files
File Type: txt query1.txt (378 Bytes, 222 views)

Reply With Quote
  #3  
Old April 26th, 2004, 08:25 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 think i can understand most part of the query...there is just one small part i dont understand

Code:
x.c_date = y.c_date


Wont this means that N_loc dont get an O_loc value if the column that have the same name matches with both a and x but just that they dont have the same C_date as y??

Example:
table y
|Name|--C_date--|
|ABCD|1/9/2003|
|BCDE|-1/1/2003-|

|Name|O_loc|--C_date--|N-loc|
|ABCD|--A--|1/1/2003|--?--|
|BCDE|--A--|-1/1/2003-|--A--|
|ABCD|--B--|1/6/2003|--?--|
|ABCD|--C--|-1/9/2003-|--C--|


Anyhow i try out the query but i got this error message
PHP Code:
 set a.new_loc = (select x.old_loc from t1 x,
                 *
ERROR at line 2:
ORA-01427single-row subquery returns more than one row 

Reply With Quote
  #4  
Old April 26th, 2004, 09:38 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
Good, you understand correctly what i meant by? to prevent the duplication of record you must use 'distint' in your query, if you have more than one record with same old_loc and c_date as well. Look at the attached query now.
Attached Files
File Type: txt query2.txt (415 Bytes, 247 views)

Reply With Quote
  #5  
Old April 27th, 2004, 03:36 AM
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
Thank you so much...after adding the distinct the query work like a charm.

Cant thank you enough...u really are a life saver...have been having many sleepless nite for the whole week trying to figure it out.

Thanks again!


YuLing

Reply With Quote
  #6  
Old April 29th, 2004, 10:02 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
why wont work for emp_id

Hi shafique...

Sorry to have bother u again, i need to do the same concept thing as i did before for o_loc to find the n_loc

Example:
|Name|-Emp_id|-C_date-|
|ABCD|--001--|1/1/2003|
|BCDE|--002--|1/1/2003|
|ABCD|--123--|1/6/2003|
|ABCD|--234--|1/9/2003|

Result in
|Name|-Emp_id|-C_date-|New_id|
|ABCD|--001--|1/1/2003|--234--|
|BCDE|--002--|1/1/2003|--002--|
|ABCD|--123--|1/6/2003|--234--|
|ABCD|--234--|1/9/2003|--234--|

I change the previous query as below:
Code:
update t2 a
set a.new_id= (select distinct x.emp_id
                         from t2 x,(select name,max(c_date) c_date
                                         from t2
                                         group by name) y
                         where x.name= y.name
                         and   y.name = a.name
                         and x.c_date=y.c_date)
/


it work find before for old location to latest location but when i do the above query to find lastest emp_id it gives me an error msg even after i put the distinct

Code:
set a.new_id= (select distinct x.emp_id
               * 
ERROR at line 2: 
ORA-01427: single-row subquery returns more than one row 


Could u please have a look at the query above to see where did i go wrong and perhaps help me to correct it??

Thanks.

Reply With Quote
  #7  
Old April 30th, 2004, 08:14 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
Please replace line 2 with the following statment:

set a.new_id= (select max(x.emp_id)

because more than one emp_id can use the same name and date. In oreder to get the most recent emp_id use max() function as shown above.


Regards,

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Find new location value??


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 6 hosted by Hostway