I have two tables: o_daily_lcsgeneration and o_daily_generation.
While trying to update the o_daily_generation I receive an error saying:
error(1111) invalid use of Group function
Here is the code I am running:
update o_daily_generation join o_daily_lcsgeneration
on o_daily_generation.Location =o_daily_lcsgeneration.Location
and o_daily_generation.Date =o_daily_lcsgeneration.Date
set o_daily_lcsgeneration.Turbine_Generation =sum(o_daily_generation.Turbine_Generation)
Try this instead:
UPDATE o_daily_generation od
INNER JOIN
(
SELECT Location, SUM(Turbine_Generation) TurbineSum
FROM o_daily_lcsgeneration
GROUP BY Location
) og ON od.Location = og.Location
SET od.Turbine_Generation = og.TurbineSum
I did update the above query based on above answer but there is an issue.
UPDATE tmpTotal t1
INNER JOIN
(
SELECT thirdlevel_delivery_id, MAX(baseline_id) MaxBaseLineId
FROM tmpTotal
GROUP BY release_id
) t2 ON t1.release_id = t2.release_id
SET t1.planned_delivery_date = t2.MaxBaseLineId;
It gives
Error Code : 1054
Unknown column 't2.release_id' in 'on clause'
Related
i tried to run this query:
WITH updateables AS (
SELECT id_product, quantity
FROM products_order
WHERE id_order = 1239 AND state = 10
)
UPDATE product
SET product.stock = updateables.quantity
WHERE product.id_product = updateables.id_product```
But this trows:
Error Code: 1054. Unknown column 'updateables.id_product' in 'where clause```
Defining a CTE using the WITH clause does not implicitly join that CTE to other tables in your query.
You would need to use a JOIN in your query to the CTE:
WITH updateables AS (
SELECT id_product, quantity
FROM products_order
WHERE id_order = 1239 AND state = 10
)
UPDATE product JOIN updateables ON product.id_product = updateables.id_product
SET product.stock = updateables.quantity;
I'm trying to update the unit_price in a sales_order_parts detail table with calculation from the applied_discount field from sales_orders. Also, the price is in the master_part_list table as price. When I run from the select statement down, it runs fine and returns the list of values from order_number 209 with the correct calculation. When I run it complete from the update line, it returns "Error Code: 1093. You can't specify target table 'sop' for update in FROM clause" Any ideas?
update sales_order_parts as sop
set unit_price =
(select (master_part_list.price * (1-(so.applied_discount/100)))
from sales_orders as so
inner join sales_order_parts as sop2
on so.id = sop2.order_id
inner join master_part_list
on sop2.part_id = master_part_list.id
where so.order_number = 209);
You could try with a join without subquery
update sales_order_parts as sop
INNER JOIN sales_orders as so on so.id = sop.order_id
AND so.order_number = 209
inner join master_part_list on sop.part_id = master_part_list.id
SET sop.unit_price = master_part_list.price * (1-(so.applied_discount/100))
Use a subquery for sales_order_parts. mysql then treats as new temporary table
Like
update sales_order_parts as sop
set unit_price =
(select (master_part_list.price * (1-(so.applied_discount/100)))
from sales_orders as so
inner join (SELECT * FROM sales_order_parts) as sop2
on so.id = sop2.order_id
inner join master_part_list
on sop2.part_id = master_part_list.id
where so.order_number = 209);
I keep getting an error when I run both of the following queries that the CUST_NUM is ambiguous. How can I fix this?
SELECT INV_NUM, CUST_NUM, CUST_LNAME, CUST_FNAME, INV_DATE, INV_AMOUNT
FROM CH08_INVOICE i
INNER JOIN CH08_CUSTOMER c1 ON (i.CUST_NUM = c1.CUST_NUM)
WHERE CUST_BALANCE>=1000;
SELECT CUST_LNAME, CUST_FNAME
FROM CH08_CUSTOMER c1 JOIN CH08_CUSTOMER_2 c2
ON (c1.CUST_LNAME = c2.CUST_LNAME AND c1.CUST_FNAME = c2.CUST_FNAME);
SELECT i.INV_NUM, i.CUST_NUM, i.CUST_LNAME, i.CUST_FNAME, i.INV_DATE, i.INV_AMOUNT FROM CH08_INVOICE i INNER JOIN CH08_CUSTOMER c1 ON (i.CUST_NUM = c1.CUST_NUM) WHERE i.CUST_BALANCE>=1000;
SELECT c1.CUST_LNAME, c1CUST_FNAME FROM CH08_CUSTOMER c1 JOIN CH08_CUSTOMER_2 c2 ON (c1.CUST_LNAME = c2.CUST_LNAME AND c1.CUST_FNAME = c2.CUST_FNAME);
Please check this query
Ambiguous column means the database don't know which table it must use the column.
Try using
SELECT INV_NUM, i.CUST_NUM ...
or
SELECT INV_NUM, c1.CUST_NUM ...
for explicity defining the table.
I am trying to write the following update statement;
UPDATE #eticat
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t
WHERE eticat_id = t.eticat_id
But it keeps complaining about ambigous columns. Can someone please tell me what I am doing wrong.
EDIT: Error Message is "Ambiguous column name 'eticat_id'."
That line is 'WHERE eticat_id = t.eticat_id'
First, that's not a CTE; it's a derived table. Similar, but different :)
Second, you're updating a table variable that's not included in your FROM clause, which is confusing SQL Server. Try something like:
UPDATE x
SET eticat_purchase_total = t.eticat_purchase_total
FROM (
SELECT eticat_id, COUNT(eticat_id) as eticat_purchase_count
FROM etransaction
INNER JOIN etransaction_item
INNER JOIN etransaction_item_catalog ON eti_eticat_id = eticat_id
ON eti_et_id = et_id
WHERE et_cmc_id = #can_cmc_id
GROUP by eticat_id
) as t JOIN #eticat x ON x.eticat_id = t.eticat_id
SELECT ma_forum.*, ma_forum_cat.*
FROM ma_forum, ma_forum_cat
JOIN ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
WHERE ma_forum.notcat_id=ma_forum_cat.notcat_id AND ma_forum.notcat_id='".$notcat_id."'
AND ma_forum.not_status='Ativo'
GROUP BY ma_forum.not_id
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC
Unknown column 'ma_forum.not_id' in 'on clause'
The column not_id does not exist in the ma_forum table.
If the column does exist, try the following and report back.
SELECT ma_forum.*,
ma_forum_cat.*
FROM ma_forum JOIN
ma_forum_cat ON ma_forum.notcat_id = ma_forum_cat.notcat_id JOIN
ma_forum_comentarios ON ma_forum_comentarios.not_id = ma_forum.not_id
WHERE ma_forum.notcat_id = '$notcat_id' AND
ma_forum.not_status= 'Ativo'
GROUP BY ma_forum.not_id
ORDER BY MAX(ma_forum_comentarios.comnot_data) DESC