Re: [SQL] Updating - Mailing list pgsql-sql

From Fomichev Michael
Subject Re: [SQL] Updating
Date
Msg-id Pine.LNX.4.04.9906171011380.352-100000@ns.region.utsr
Whole thread Raw
List pgsql-sql

On Wed, 16 Jun 1999, Bob Kruger wrote:

> 
> Thanks for all who assisted with my previous question on grouping.
> 
> I would like to do an update of a table with the results of a query on
> another.
> 
> For example:
> 
> table vehcost
> 
> id        serial
> po        varchar(12)
> veh_no        varchar(8)
> cost        real
> comments    varchar(30)
> 
> 
> table vehinfo
> 
> id            serial
> veh_no        varchar(12)
> m_cost        real
> ...
> 
> 
> I would like to take the sum of vehcost.veh_no and update that info to
> field vehinfo.m_cost.
> 
> So far, I have worked with the following without success:
> 
> update vehinfo set m_cost = (select sum(vehcost.cost) from vehcost group by
> veh_no) where vehinfo.veh_no = vehcost.veh_no ; 

This query has an error. You can't use a subquery in `set m_cost = '
expression. So, I think you can't update your table with one query. 
You need something like this:

create view tmp as select veh_no, sum(cost) from vehcost group by veh_no;

Now you have a view like your subquery.

And after that:

update vehinfo set m_cost = tmp.sum from tmp where
vehinfo.veh_no=tmp.veh_no;

Remember `from tmp' clause is PostgreSQL non-standard extension in SQL.  

May be there are another solutions.

Mike.




pgsql-sql by date:

Previous
From: Fomichev Michael
Date:
Subject: Re: [SQL] Update with two tables??
Next
From: "Henry B. Hotz"
Date:
Subject: Re: [HACKERS] Postgres mailing lists