Re: [SQL] Updating

From: Fomichev Michael <fomichev(at)null(dot)ru>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] Updating
Date: 1999-06-16 21:12:39
Message-ID: Pine.LNX.4.04.9906171011380.352-100000@ns.region.utsr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

Browse pgsql-sql by date

  From Date Subject
Next Message Fomichev Michael 1999-06-16 21:15:11 Re: [SQL] Update with two tables??
Previous Message Jackson, DeJuan 1999-06-16 20:35:46 RE: [SQL] Little Help is needed.