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 > Re: update with...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 3449 of 3661
Post > Topic >>

Re: update with multiple fields as aggregates

by yazicivo@[EMAIL PROTECTED] (Volkan YAZICI) May 3, 2008 at 05:36 PM

On Sat, 3 May 2008, Alexy Khrabrov <deliverable@[EMAIL PROTECTED]
> writes:
> 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

I didn't try but, here is my 2 cents:

  UPDATE rats
     SET of1 = tmp.of1,
         sumof1 = tmp.sumof1
    FROM (SELECT id, AVG(o) AS of1, SUM(o) AS sumof1
            FROM rats,
                 offset1
        GROUP BY id)
      AS tmp
   WHERE tmp.id = rats.id;


Regards.

-- 
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
tan12V112 Sat Sep 6 15:47:08 CDT 2008.