Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > update with mul...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 3449 of 3596
Post > Topic >>

update with multiple fields as aggregates

by deliverable@[EMAIL PROTECTED] (Alexy Khrabrov) May 3, 2008 at 01:05 AM

I need to fill two columns of a Rats table from an Offset1 table,  
where for each Id row in Rats we need to fill an average offset and  
the sum of all offset from Offset1 with the same Id.  I can create a  
derivative table like this:

create table ofrats as (select customer_id as cid,avg(o),sum(o) from  
offset1 group by cid);

But if I want to insert the two values into the Rats directly, I get  
an error:

netflix=> update rats r1 set of1=s.ao, sumof1=s.so from (select avg(o)  
as ao,sum(o) as so from rats,offset1 o1 where o1.customer_id=r1.id  
group by id) as s;
ERROR:  subquery in FROM cannot refer to other relations of same query  
level

-- is there a way to formulate the subquery for Update properly here  
so it's as efficient as the transfer table above?  Currently I have to  
create that auxiliary table and then transfer values into Rats via Id,  
then drop the table -- is it an idiom too or there's a better way to  
do it?

Cheers,
Alexy

-- 
Sent via pgsql-sql mailing list (pgsql-sql@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
 




 2 Posts in Topic:
update with multiple fields as aggregates
deliverable@[EMAIL PROTEC  2008-05-03 01:05:07 
Re: update with multiple fields as aggregates
yazicivo@[EMAIL PROTECTED  2008-05-03 17:36:49 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan13V112 Thu Jul 24 6:39:19 CDT 2008.