I have trouble with my query which I want to insert and update while it is duplicate, but it said "Invalid use of group function" instead.
I have run my only "Select" statement and there was no issue like "Invalid use of group function".
here is my full code :
INSERT INTO tbl_biir_aktual(cabang_kode, periode_thn, periode_bln, pending_pp_volume, pending_pp_value)
SELECT a.cabang_kode, YEAR(a.tanggal) AS tahun, MONTH(a.tanggal)AS bulan,
SUM(a.qty_pending*a.unit_barang)AS tonase_pending, SUM(a.value_pending)AS value_pending
FROM tbl_order a,
(SELECT b.cabang_kode, MAX(b.tanggal)tanggal
FROM tbl_order b
GROUP BY b.cabang_kode, YEAR(b.tanggal), MONTH(b.tanggal)) AS max_cabang
WHERE max_cabang.cabang_kode = a.cabang_kode AND max_cabang.tanggal = a.tanggal
GROUP BY cabang_kode, YEAR(tanggal), MONTH(tanggal)
ON DUPLICATE KEY
UPDATE pending_pp_volume = SUM(a.qty_pending*a.unit_barang), pending_pp_value = SUM(a.value_pending);
well,
Hey I just found this MySQL ON DUPLICATE KEY UPDATE while inserting a result set from a query
INSERT INTO tbl_biir_aktual(cabang_kode, periode_thn, periode_bln, pending_pp_volume, pending_pp_value)
SELECT a.cabang_kode, YEAR(a.tanggal) AS tahun, MONTH(a.tanggal)AS bulan,
#tonase_pending := SUM(a.qty_pending*a.unit_barang)AS tonase_pending, #value_pending := SUM(a.value_pending)AS value_pending
FROM tbl_order a,
(SELECT b.cabang_kode, MAX(b.tanggal)tanggal
FROM tbl_order b
GROUP BY b.cabang_kode, YEAR(b.tanggal), MONTH(b.tanggal)) AS max_cabang
WHERE max_cabang.cabang_kode = a.cabang_kode AND max_cabang.tanggal = a.tanggal
GROUP BY cabang_kode, YEAR(tanggal), MONTH(tanggal)
ON DUPLICATE KEY
UPDATE pending_pp_volume = #tonase_pending, pending_pp_value = #value_pending;
I've tried that and it's done.
Tq Edper for your comment anyway....
Try using the alias in your update:
UPDATE SET pending_pp_volume = tonase_pending, pending_pp_value = value_pending
Also it's more wise to use another name for your alias for your aggregate than using the same name as your existing field name like the value_pending. Might as well changed it to TotalValuePending, so that it would be:
UPDATE SET pending_pp_volume = tonase_pending, pending_pp_value = TotalValuePending
Related
I have read a bunch of ways that has gotten me this far. But I can't get to the finish line.
I have a table of coupon codes. I want to use one transaction to select the next available code, mark it as used and input the order number. I can get the update and nested select to work, but I cannot figure out how to actually return the coupon code from the select. It just returns 1 row updated.
Here's what I've got:
UPDATE `prcoupon` pr
SET
`pr`.`status` = '1',
`pr`.`invoicenumber` = '09990002'
WHERE
`pr`.`couponCode` = (SELECT
`prcoupon`.`couponcode`
FROM
`prcoupon`
WHERE
`status` = 0
LIMIT 1)
Sample data
What I need returned is: couponCode: SL2T-03A0-JVCY-W2XMXG
If I understand correctly, you can try to use UPDATE ... JOIN with ROW_Nunber windwon function.
UPDATE prcoupon pr
JOIN (
SELECT *,ROW_NUMBER() OVER(ORDER BY couponCode) rn
FROM prcoupon
WHERE status = 0
) t2 ON pr.couponcode = t2.couponcode
SET pr.status = 1,
pr.invoicenumber = '09990002'
WHERE rn = 1
sqlfiddle
I am trying to alter my code so when there is no row with a matching ean13 in webshop_stock it needs to INSERT a new row.
MYSQL is currently not accepting my code. i have tried a few things in order to get it working. My search on the worldwide-web did not find a good example with INSERT INTO - JOIN - ON DUPLICATE KEY UPDATE. So my question is, is it possible?
The problem at this moment is that my rows get created when they not exist, but the rows that exist does not get updated.
Tested the following code:
INSERT INTO webshop_stock
(id_warehouse,id_product,id_product_attribute,ean13, physical_quantity, usable_quantity)
SELECT
'1',
pa.id_product,
pa.id_product_attribute,
pa.ean13,
ai.quantity,
ai.quantity
FROM
webshop_product_attribute pa,
Adcount_input ai
WHERE
pa.ean13 = ai.ean13
AND NOT EXISTS
(SELECT id_product_attribute FROM webshop_stock
WHERE id_product_attribute = pa.id_product_attribute)
ON DUPLICATE KEY UPDATE
physical_quantity = ai.quantity,
usable_quantity = ai.quantity
Original code:
UPDATE
webshop_stock AS s
JOIN(
SELECT
pa.ean13,
pa.id_product_attribute,
pa.id_product,
ai.quantity
FROM
webshop_product_attribute pa,
Adcount_input ai
WHERE
pa.ean13=ai.ean13) q
SET
s.id_warehouse = 1,
s.id_product = q.id_product,
s.id_product_attribute = q.id_product_attribute,
s.ean13 = q.ean13,
s.physical_quantity = q.quantity,
s.usable_quantity = q.quantity
WHERE
s.id_product_attribute = q.id_product_attribute
SOLVED this code does what i need.
INSERT INTO webshop_stock
(id_warehouse, id_product, id_product_attribute, ean13, physical_quantity, usable_quantity)
SELECT
'1',
pa.id_product,
pa.id_product_attribute,
pa.ean13,
ai.quantity,
ai.quantity
FROM
webshop_product_attribute pa,
Adcount_input ai
WHERE
pa.ean13 = ai.ean13
ON DUPLICATE KEY UPDATE
physical_quantity = ai.quantity, usable_quantity = ai.quantity
The syntax is:
INSERT <table> <field list>
SELECT ... JOIN ...
There is NO INSERT JOIN SELECT in mysql. It's INSERT SELECT JOIN.
http://dev.mysql.com/doc/refman/5.1/en/insert-select.html
There are 2 tables INFO and ADDR
ADDR has 4 columns ID, LINE_1_ADDR, LINE_2_ADDR, TEMP_ADDR_ID
INFO has 4 columns ID, FIRST_NAME, LAST_NAME, ADDR_ID
I want to replace the data in INFO.ADDR_ID with ADDR.ID whenever there is a match of INFO.ADDR_ID and ADDR.TEMP_ADDR_ID
I have the following UPDATE query which is giving error
ORA-00905: missing keyword
Below is my code:
UPDATE INFO SET INFO.ADDR_ID = (CASE
WHEN ADDR.TEMP_ADDR_ID = INFO.ADDR_ID
THEN INFO.ADDR_ID = ADDR.ID
END);
I am new to SQL queries involving case, don't know where it's wrong. Any help please !
Thanks
if you using MYSQL
UPDATE INFO
INNER JOIN ADRR ON ADDR.TEMP_ADDR_ID = INFO.ADDR_ID
SET INFO.ADDR_ID = ADDR.ID
If you using ORACLE
use this
UPDATE INFO SET INFO.ADDR_ID = (SELECT ADRR.ID
FROM ADRR
WHERE ADDR.TEMP_ADDR_ID = INFO.ADDR_ID)
EDIT: After getting more info, echo_Me's approach is the right one for the update.
UPDATE INFO
JOIN ADDR ON /*your Join condition*/
SET INFO.ADDR_ID = ADDR.ID
I'm trying to update a table based on 2 select subquery that will be multipied to produce the value for Harga column
here is my code :
UPDATE bahanmakanan
SET Harga = (SELECT HargaSatuan from detail_bahanmakanan
WHERE IDBahanMakanan = "BM01")* (SELECT jumlah from bahanmakanan
WHERE IDBahanMakanan = "BM01")
WHERE IDBahanMakanan = "BM01" ;
The error message return
Error Code: 1093. You can't specify target table 'bahanmakanan' for update in FROM clause
you can simply do this using JOIN,
UPDATE bahanmakanan a
INNER JOIN detail_bahanmakanan b
ON a.IDBahanMakanan = b.IDBahanMakanan
SET a.Harga = a.jumlah * b.HargaSatuan
WHERE a.IDBahanMakanan = 'BM01'
Please do backup first your database before executing the statement.
Try this:
UPDATE bahanmakanan as t1
JOIN detail_bahanmakanan as t2 USING(IDBahanMakanan)
SET t1.Harga = t2.HargaSatuan * t1.jumlah
WHERE IDBahanMakanan = "BM01";
I got the following query :
INSERT INTO contracts_settings (contract_id, setting_id, setting_value)
VALUES (:contract_id, (
SELECT setting_id
FROM settings
WHERE setting_type = :setting_type
AND setting_name = :setting_name
LIMIT 1
), :setting_value)
ON DUPLICATE KEY UPDATE setting_value = :setting_value
The value with the prefix : is replaced with data using PHP PDO::bindBalue.
If the inner query find nothing (it return NULL) but also INSERT a NULL statement. How to avoid that ?
Thanks.
Convert the INSERT ... VALUES syntax to INSERT ... SELECT:
INSERT INTO contracts_settings
(contract_id, setting_id, setting_value)
SELECT
:contract_id,
setting_id,
:setting_value
FROM settings
WHERE setting_type = :setting_type
AND setting_name = :setting_name
LIMIT 1
ON DUPLICATE KEY UPDATE
setting_value = :setting_value ;