![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
customerinvoiceid (...) ordercharges: orderchargeid +---orderid |
#2
| |||
| |||
|
|
You may only UPDATE one table at a time, you can't update a JOIN. So when selecting from another table to filter or calculate your update, the form is: UPDATE orderchanges SET orderchargesbilled = {expression} FROM orders WHERE orders.orderid = ordercharges.orderid AND etc. |
#3
| |||
| |||
|
|
Thanks for the suggestions everyone, however I'm still at the same underlying stopping point: the subselect in the SET clause returns multiple rows, and I don't know how to make it 'iterate' on each orderid in the specified customerinvoiceid without using a JOIN, which is itself apparently either not directly possible or complex. UPDATE ordercharges SET orderchargeasbilled = (expression) WHERE ordercharges.orderchargecode = 'S&H' and ordercharges.orderid=(SELECT orderid (tried IN(SELECT...) as well) FROM orders WHERE customerinvoiceid = '54321'); 'expression' needs to get the orderchargeasbilled for the current orderid |
#4
| |||
| |||
|
|
The key point is that you have to deal with two instances of the ordercharges table, one having orderchargecode = 'S&H' (this is the one you want to update), the other one having orderchargecode = 'SALE' which is where the values come from. UPDATE ordercharges SET orderchargeasbilled = 0.065 * sale.orderchargeasbilled FROM orders AS o, ordercharges AS sale WHERE ordercharges.orderchargecode = 'S&H' AND ordercharges.orderid = o.orderid AND sale.orderchargecode = 'SALE' AND sale.orderid = o.orderid AND o.customerinvoiceid = '54321'; |
#5
| |||
| |||
|
|
UPDATE ordercharges SET orderchargeasbilled = 0.065 * sale.orderchargeasbilled FROM orders AS o, ordercharges AS sale WHERE ordercharges.orderchargecode = 'S&H' AND ordercharges.orderid = o.orderid AND sale.orderchargecode = 'SALE' AND sale.orderid = o.orderid AND o.customerinvoiceid = '54321'; I'd like to think I would have gotten to this eventually, but I doubt it. |
|
What I came up with was deleting and reinserting the relevant ordercharges rows |
|
SELECT oc.orderchargeid, oc.orderid, oc.orderchargecode, 0.065 * oc2.orderchargeasbilled AS orderchargeasbilled FROM ordercharges AS oc, ordercharges AS oc2, orders AS o WHERE oc.orderid = o.orderid AND o.customerinvoiceid = '54321' AND oc.orderchargecode = 'S&H' AND oc.orderid = oc2.orderid AND oc2.orderchargecode = 'SALE'; |
#6
| ||||
| ||||
|
|
What I came up with was deleting and reinserting the relevant ordercharges rows This might have unwanted side effects (think ON DELETE CASCADE). |

|
You already have: SELECT oc.orderchargeid, oc.orderid, oc.orderchargecode, 0.065 * oc2.orderchargeasbilled AS orderchargeasbilled FROM ordercharges AS oc, ordercharges AS oc2, orders AS o WHERE oc.orderid = o.orderid AND o.customerinvoiceid = '54321' AND oc.orderchargecode = 'S&H' AND oc.orderid = oc2.orderid AND oc2.orderchargecode = 'SALE'; To transform this into an UPDATE statement (which is not standard SQL, BTW) |
|
First, the target table of the UPDATE operation cannot have an alias. snip/ Second, we don't care about how output expressions are named, so we remove that alias, too. snip/ Third, Postgres implicitly adds the target table to the FROM clause, so we move it from the FROM clause to after the command verb, when we change SELECT to UPDATE. |
|
UPDATE ordercharges SET orderchargeid = ordercharges.orderchargeid, orderid = ordercharges.orderid, orderchargecode = ordercharges.orderchargecode, orderchargeasbilled = 0.065 * oc2.orderchargeasbilled FROM ordercharges AS oc2, orders AS o WHERE ordercharges.orderid = o.orderid AND o.customerinvoiceid = '54321' AND ordercharges.orderchargecode = 'S&H' AND ordercharges.orderid = oc2.orderid AND oc2.orderchargecode = 'SALE'; Finally we remove the redundant a=a assignments and get: UPDATE ordercharges SET orderchargeasbilled = 0.065 * oc2.orderchargeasbilled FROM ordercharges AS oc2, orders AS o WHERE ordercharges.orderid = o.orderid AND o.customerinvoiceid = '54321' AND ordercharges.orderchargecode = 'S&H' AND ordercharges.orderid = oc2.orderid AND oc2.orderchargecode = 'SALE'; ... which looks and behaves like what I posted before. |
#7
| |||
| |||
|
|
What I came up with was deleting and reinserting the relevant ordercharges rows This might have unwanted side effects (think ON DELETE CASCADE). Good point. At this stage in my PostgreSQL progress, I haven't been using ON DELETE CASCADE |
|
Third, Postgres implicitly adds the target table to the FROM clause, so we move it from the FROM clause to after the command verb, when we change SELECT to UPDATE. I've noticed in SELECT queries when I've neglected to include a table in the FROM clause but named it specifically in an attribute that PostgreSQL's added it. |
![]() |
| Thread Tools | |
| Display Modes | |
| |