View Full Version : New to SQL Analytics and need help
Scott Johnson
09-23-2004, 05:21 AM
I'm fairly new to sql analytics and need help with an relatively simple
problem.
Given table:
ID Operation Date Old_Loc New_Loc
100 9/1/04 12:34 B1
100 9/1/04 12:45 B2
100 9/1/04 13:30
I want to move the location down by one record so the result will be
ID Operation Date Old_Loc New_Loc
100 9/1/04 12:34 B1
100 9/1/04 12:45 B2 B1
100 9/1/04 13:30 B2
I'm using:
update tab_name
set new_loc = (select lead(old_loc) over (partition by id order by
operation_dt) from tab_name);
but I'm getting Ora 1427 - Single-row subquery returns more than one row.
Like I said, I'm new to sql analytics so I may be completely off base.
Can anyone offer some insight?
Thanks,
Scott
Pratap
09-24-2004, 01:42 AM
You have to lead LAG instead of LEAD
To update, if you have few rows then this will work fine -
begin
for rec in
(
select lead(old_loc) over (partition by id order by operation_dt)
a, rowid rid
from tab_name
)
loop
update tab_name set new_loc = rec.a where rowid = rid
end loop;
end;
But if you have too many rows and performance is an issue then use
merge command as given here -
http://groups.google.co.in/groups?hl=en&lr=&ie=UTF-8&threadm=830861d2.0409230400.3a1ea0cd%40posting.google.com&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26group%3Dcomp.databases.oracle.server
Regards,
Pratap
MyLounge.com Site Map
Forum:
Cars,
Cell Phone,
Database,
Games,
Home Improvement,
IT,
Music,
School,
Sports,
Web Design,
Web Server,
Weight Loss
The MyLounge.com forum is intended for informational use only and should not
be relied upon and is not a substitute for any advice. The information contained
on MyLounge.com are opinions and suggestions of members and is not a representation
of the opinions of MyLounge.com. MyLounge.com does not warrant or vouch for
the accuracy, completeness or usefulness of any postings or the qualifications
of any person responding. Please consult a expert or seek the services of an
attorney in your area for more accuracy on your specific situation. Please note
that our forums also serve as mirrors to Usenet newsgroups. Many posts you see
on our forums are made by newsgroup users who may not be members of MyLounge.com
Term of Service
vBulletin v3.0.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.