473,387 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

UPDATE table to a joined query...

Hello all,

This question is related to updating tables - is there any way to calculate or
update the values in a column in a table to the values in a field produced by
a query result? An example of what I'm trying to do is below:

update (tbl_ind_mananas LEFT JOIN (select count(*) as count, (dubicacion ||
zona || manzana) as cod_manzana from tbl_censo_poblacion_1993 group by
dubicacion, zona, manzana) tbl1 on relacion = cod_manzana) as tbl2 set
poblacion = count;

Basically I have a table (tbl_ind_manzanas) with a unique code (relacion) that
can be linked to a field in the query result (cod_manzana). I want to update
a field in the table with the count(*) result in the query. The update
statement (as I have attempted it above) doesn't work...it seems that I can do
nothing but directly update the values in a table. As soon as I try to supply
anything other than just a table to the update statement, it doesn't like it.

I'm sure others have experience with this issue...it seems to me that there
would be many cases where such an approach would be useful. I'm hoping
there's another method that I might be able to use that could accomplish
essentially the same result. Any suggestions are greatly appreciated.

Kind regards,
Mike
----------------------------------------
This mail sent through www.mywaterloo.ca

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #1
2 8480
On Wed, Oct 15, 2003 at 22:39:39 -0400,
Mike Leahy <mg*****@fes.uwaterloo.ca> wrote:
Hello all,

This question is related to updating tables - is there any way to calculate or
update the values in a column in a table to the values in a field produced by
a query result? An example of what I'm trying to do is below:

update (tbl_ind_mananas LEFT JOIN (select count(*) as count, (dubicacion ||
zona || manzana) as cod_manzana from tbl_censo_poblacion_1993 group by
dubicacion, zona, manzana) tbl1 on relacion = cod_manzana) as tbl2 set
poblacion = count;

Basically I have a table (tbl_ind_manzanas) with a unique code (relacion) that
can be linked to a field in the query result (cod_manzana). I want to update
a field in the table with the count(*) result in the query. The update
statement (as I have attempted it above) doesn't work...it seems that I can do
nothing but directly update the values in a table. As soon as I try to supply
anything other than just a table to the update statement, it doesn't like it.

I'm sure others have experience with this issue...it seems to me that there
would be many cases where such an approach would be useful. I'm hoping
there's another method that I might be able to use that could accomplish
essentially the same result. Any suggestions are greatly appreciated.


A similar question was asked in the last week.

You want to do the join in the from item list and than join the table
being updated to the join from the from item list in the where clause.

Something like:
update a set a.count = c.count from (select a left join b) as c
where a.id = c.id;

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 12 '05 #2
You appear to have accidentally just replied to me.

On Thu, Oct 16, 2003 at 12:48:35 -0400,
Mike Leahy <mg*****@fes.uwaterloo.ca> wrote:
Okay...that seems to work. However, there are two things that are causing
me problems. First, if I use the statement exactly as you have it in your
reply I get 'ERROR: parser: parse error at or near "." at character 15' -
it doesn't seem to like the 'a.' before the first reference to the column
being named. It's clearly nothing serious - I suppose it's redundant to put
it there anyway, since we've already indicated we're updating the columns in
table 'a' to begin with.
That was my mistake. A table name there makes no sense since as you noted
the column has to be in the table being updated.
Second, update I'm trying to run is about 20000 records (both in the table
being updated, and in the joined query). When I run this update, there is
the usual length of time to run the query itself, then when it begins
updating my processor sits at 100% for about 15 minutes (with a 2.4 GHz
processor). Is this normal, or is my update statement structured poorly
somehow? I tried dumping the query to a table rather than executing it
within the update statement, and that didn't make much difference (example
below).
An explain analyze would probably be useful to see.
update tbl_ind_manzanas set poblacion = c.thecount from (select cod_manzana,
thecount from tbl_ind_manzanas left join tmp_query on
tbl_ind_manzanas.cod_manzana = tmp_query.cod_manz) as c where
tbl_ind_manzanas.cod_manzana = c.cod_manzana;

Can anyone suggest how this might possibly be improved so that it isn't so
computationally intensive?
You probably want want to create an index on tmp_query (cod_manz) (and of
course (tbl_ind_manzanas (cod_manzana)) so that you don't need to do a sort
or nestloop to do the left join.

At any rate...it seems to work fine despite the length of time to execute.
Thanks for the help Bruno.

Mike
-----Original Message-----
From: Bruno Wolff III [mailto:br***@wolff.to]
Sent: October 16, 2003 10:30 AM
To: Mike Leahy
Cc: pg***********@postgresql.org
Subject: Re: [GENERAL] UPDATE table to a joined query...

On Wed, Oct 15, 2003 at 22:39:39 -0400,
Mike Leahy <mg*****@fes.uwaterloo.ca> wrote:
Hello all,

This question is related to updating tables - is there any way to

calculate or
update the values in a column in a table to the values in a field produced

by
a query result? An example of what I'm trying to do is below:

update (tbl_ind_mananas LEFT JOIN (select count(*) as count, (dubicacion

||
zona || manzana) as cod_manzana from tbl_censo_poblacion_1993 group by
dubicacion, zona, manzana) tbl1 on relacion = cod_manzana) as tbl2 set
poblacion = count;

Basically I have a table (tbl_ind_manzanas) with a unique code (relacion)

that
can be linked to a field in the query result (cod_manzana). I want to

update
a field in the table with the count(*) result in the query. The update
statement (as I have attempted it above) doesn't work...it seems that I

can do
nothing but directly update the values in a table. As soon as I try to

supply
anything other than just a table to the update statement, it doesn't like

it.

I'm sure others have experience with this issue...it seems to me that

there
would be many cases where such an approach would be useful. I'm hoping
there's another method that I might be able to use that could accomplish
essentially the same result. Any suggestions are greatly appreciated.


A similar question was asked in the last week.

You want to do the join in the from item list and than join the table
being updated to the join from the from item list in the where clause.

Something like:
update a set a.count = c.count from (select a left join b) as c
where a.id = c.id;


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sqlgoogle | last post by:
Hi I'm having update problem. Here is the senario I have to different db server (SQL Server) linked with each other In DB Server 1 I have 2 tables & In DB Server 2 I have 3 tables. I have...
2
by: Fons Roelandt | last post by:
Heelo, I have to Update all fields from a table with the values of a related table, i've tried some querys i found on the internet, but nothing seems to word, i even tried to lookup the value...
4
by: John Baker | last post by:
Hi: I have two tables, a setup table (TblSetup) and a purchase order table (tblPO). When i construct a query with ONLY the tblPO shown, and a type in parameter for the PO number, I an update...
4
by: deko | last post by:
I'm trying to update the address record of an existing record in my mdb with values from another existing record in the same table. In pseudo code it might look like this: UPDATE tblAddress SET...
2
by: NigelMThomas | last post by:
I have an especially challenging problem. I know there are a few geniuses in this group so perhaps; you can advise me whether or not this can be done as an update query in Access. Thanks. I am...
1
by: Foef | last post by:
When I have a stored procedure, with multiple tables in MS Access, in a dataset and I change it in my DataGrid, I get the next message when I want to update my DataSet: ...
3
by: turtle | last post by:
I have Two tables (Table1 and Table2). Both tables have a common field called part number. Table 1 contains an extra field that i would like to update table 2 to match if the part number matches....
1
by: racquetballer | last post by:
I need to to an update query that involves three tables: table Dealer needs to be updated with data from table Personnel and table Title. Dealer is joined to Personnel where Dealer.Dealer_Code =...
2
by: joeyrhyulz | last post by:
Hi, I'm trying to make a very simple update statement (in Oracle) in jet sql that seems much more difficult than it should be. The root of my problem is that I'm trying to update a field on a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.