473,399 Members | 3,888 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,399 software developers and data experts.

update query confusion

The following query updated all the rows in the
AssembliesBatch table, not just where batchID=5.

There are 2 rows in the AssembliesBatch table with batch ID of
5 and I wanted to update both of them with their price, based
on the data in the from clause. One row has 105 units and the
other row has 2006 units. the active price in both rows is 6.6
and the pricedifferential is 0. My expectation is that the
first row would be updated to 693 and the second to be updated
to 13239.6. Instead every row in the table was updated to 693.

This syntax works in MS SQL Server to update exactly as I
expected, with the difference that you have to use the
aliasname after the update keyword and postgresql does not
allow that.
If anyone can help, I would greatly appreciate it.

update AssembliesBatch set BuildPrice=a.units*(coalesce(ActivePrice,0) + coalesce(PriceDifferential,0))
from AssembliesBatch a join assemblies b on a.AssemblyID=b.assemblyID
left join qry_AssemblyPrices c on c.AssemblyID=b.assemblyID
left join ProductQuantityPrice d on d.ProductID=b.ProductID
inner join qry_TotalBatchProductCards e on e.ProductID=b.ProductID and e.BatchID=a.BatchID
and e.TotalCards between minquantity and maxquantity
where a.BatchID=5;

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax
---------------------------(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 23 '05 #1
2 2594
Ok. I got it working by adding
"and assembliesBatch.AssembliesBatchID=a.AssembliesBatc hID"
to the where clause. This seems a bit awkward sytactically. Is there a
cleaner way of doing it?

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax

__________________________________________________ ______________________________

The following query updated all the rows in the
AssembliesBatch table, not just where batchID=5.

There are 2 rows in the AssembliesBatch table with batch ID of
5 and I wanted to update both of them with their price, based
on the data in the from clause. One row has 105 units and the
other row has 2006 units. the active price in both rows is 6.6
and the pricedifferential is 0. My expectation is that the
first row would be updated to 693 and the second to be updated
to 13239.6. Instead every row in the table was updated to 693.

This syntax works in MS SQL Server to update exactly as I
expected, with the difference that you have to use the
aliasname after the update keyword and postgresql does not
allow that.
If anyone can help, I would greatly appreciate it.

update AssembliesBatch set BuildPrice=a.units*(coalesce(ActivePrice,0) + coalesce(PriceDifferential,0))
from AssembliesBatch a join assemblies b on a.AssemblyID=b.assemblyID
left join qry_AssemblyPrices c on c.AssemblyID=b.assemblyID
left join ProductQuantityPrice d on d.ProductID=b.ProductID
inner join qry_TotalBatchProductCards e on e.ProductID=b.ProductID and e.BatchID=a.BatchID
and e.TotalCards between minquantity and maxquantity
where a.BatchID=5;

Thank You
Sim Zacks
IT Manager
CompuLab
04-829-0145 - Office
04-832-5251 - Fax
---------------------------(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
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
Sim Zacks <si*@compulab.co.il> writes:
This syntax works in MS SQL Server to update exactly as I
expected, with the difference that you have to use the
aliasname after the update keyword and postgresql does not
allow that.
If anyone can help, I would greatly appreciate it. update AssembliesBatch set BuildPrice=a.units*(coalesce(ActivePrice,0) + coalesce(PriceDifferential,0))
from AssembliesBatch a join assemblies b on a.AssemblyID=b.assemblyID
left join qry_AssemblyPrices c on c.AssemblyID=b.assemblyID
left join ProductQuantityPrice d on d.ProductID=b.ProductID
inner join qry_TotalBatchProductCards e on e.ProductID=b.ProductID and e.BatchID=a.BatchID
and e.TotalCards between minquantity and maxquantity
where a.BatchID=5;


I believe that SQL Server identifies the target table (AssembliesBatch)
with "AssembliesBatch a", whereas Postgres does not, turning this into
an unconstrained self-join. You need to do something more like

update AssembliesBatch set BuildPrice=AssembliesBatch.units*(coalesce(ActiveP rice,0) + coalesce(PriceDifferential,0))
from assemblies b
left join qry_AssemblyPrices c on c.AssemblyID=b.assemblyID
left join ProductQuantityPrice d on d.ProductID=b.ProductID
inner join qry_TotalBatchProductCards e on e.ProductID=b.ProductID and e.BatchID=AssembliesBatch.BatchID
and e.TotalCards between minquantity and maxquantity
where AssembliesBatch.AssemblyID=b.assemblyID
and AssembliesBatch.BatchID=5;

If we supported an alias for the update target table you could
write this as

update AssembliesBatch a set BuildPrice=a.units*(coalesce(ActivePrice,0) + coalesce(PriceDifferential,0))
from assemblies b
left join qry_AssemblyPrices c on c.AssemblyID=b.assemblyID
left join ProductQuantityPrice d on d.ProductID=b.ProductID
inner join qry_TotalBatchProductCards e on e.ProductID=b.ProductID and e.BatchID=a.BatchID
and e.TotalCards between minquantity and maxquantity
where a.AssemblyID=b.assemblyID
and a.BatchID=5;

which is a bit less typing but not fundamentally different.
However, the SQL spec does not allow an alias there and at
present we have not decided to extend the spec in this
particular direction.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #3

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

Similar topics

8
by: Perre Van Wilrijk | last post by:
Hello, I have 2 ways of updating data I'm using often 1) via a cursor on TABLE1 update fields in TABLE2 2) via an some of variables ... SELECT @var1=FLD1, @var2=FLD2 FROM TABLE1 WHERE...
1
by: AAVF | last post by:
I have a small table that need to keep a balance of work outstanding. The fields are as follows REC_COUNT (auto-index created record counter) DATE (work date) QTY_REQD (work added on that date)...
8
by: Maxi | last post by:
There is a lotto system which picks 21 numbers every day out of 80 numbers. I have a table (name:Lotto) with 22 fields (name:Date,P1,P2....P21) Here is the structure and sample data: ...
7
by: Mark Carlyle via AccessMonster.com | last post by:
I have this update query that I am trying to run. I know the syntax is messed up but do not know how to correct it. Select 'UPDATE', Transactions,'Set = where = ' From "Get Daily Balances" ...
8
by: rriness | last post by:
I'm getting an inconsistent failure when trying to save data in ADO.Net. I'm using an Access database with a simple query - SELECT StudentID, FirstName, LastName FROM Students - and have no...
11
by: diablo | last post by:
Hi i need help with formulating a query that will update the a field on one table depending on the values from another for example i have a cart table: cartid, buyerid, productid, quantity ...
0
by: Krij | last post by:
Hi! In my database I have a wish to return certain data to tblItemAmount, which is related to tblItem: tblItem(one)->tblItemAmount(many) On tblOrderDetails I have a return field (Yes/No). ...
3
by: Thorben Grosser | last post by:
Hello Newsgroup, I am doing some archive database and therefore got one table indexing every folder and one table storing which rack belongs to which department, eg: table folders :...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.