I'm getting a confusing "ORA97100 missing SET keyword" error when trying to run this simple UPDATE statement.
UPDATE
(SELECT
RKAP_PROYEKSI.ID AS ID,
RKAP_PROYEKSI.TAHUN AS TAHUN,
RKAP_PROYEKSI.KODE_ANGGARAN AS KODE_ANGGARAN,
RKAP_PROYEKSI.JENIS_BIAYA AS JENIS_BIAYA,
RKAP_PROYEKSI.SUBTOTAL AS SUBTOTAL,
RKAP_PROYEKSI.TOTAL AS TOTAL,
RKAP_PROYEKSI.BELONGS_TO AS BELONGS_TO,
RKAP_PROYEKSI.NOMOR AS NOMOR,
RKAP_PROYEKSI.STATUS AS STATUS
FROM WOS.RKAP_PROYEKSI
LEFT JOIN WOS.RKAP_MASTER_KODE ON RKAP_MASTER_KODE.ID_KODE = RKAP_PROYEKSI.KODE_ANGGARAN
LEFT JOIN WOS.RKAP_USER ON RKAP_USER.BIRO = RKAP_MASTER_KODE.BIRO
WHERE TAHUN = '2018' AND RKAP_MASTER_KODE.BIRO = 'BSI') AS helper
SET helper.STATUS = 0
I've also tried to erase the "AS", but still get in error.
Any well thought to advise will be appreciated.
Thanks
You can use WITH..AS clause :
UPDATE RKAP_PROYEKSI R
SET STATUS = (
WITH RP AS
(
SELECT
RKAP_PROYEKSI.ID AS ID,
RKAP_PROYEKSI.TAHUN AS TAHUN,
RKAP_PROYEKSI.KODE_ANGGARAN AS KODE_ANGGARAN,
RKAP_PROYEKSI.JENIS_BIAYA AS JENIS_BIAYA,
RKAP_PROYEKSI.SUBTOTAL AS SUBTOTAL,
RKAP_PROYEKSI.TOTAL AS TOTAL,
RKAP_PROYEKSI.BELONGS_TO AS BELONGS_TO,
RKAP_PROYEKSI.NOMOR AS NOMOR,
RKAP_PROYEKSI.STATUS AS STATUS,
0 AS ZERO_STATUS
FROM WOS.RKAP_PROYEKSI
LEFT JOIN WOS.RKAP_MASTER_KODE ON RKAP_MASTER_KODE.ID_KODE = RKAP_PROYEKSI.KODE_ANGGARAN
LEFT JOIN WOS.RKAP_USER ON RKAP_USER.BIRO = RKAP_MASTER_KODE.BIRO
WHERE TAHUN = '2018' AND RKAP_MASTER_KODE.BIRO = 'BSI')
SELECT RP.ZERO_STATUS
FROM RP
WHERE RP.ID = R.ID
)
I think you can phrase this as:
UPDATE WOS.RKAP_PROYEKSI p
SET STATUS = 0
WHERE p.TAHUN = '2018' AND
EXISTS (SELECT 1
FROM WOS.RKAP_MASTER_KODE mk JOIN
WOS.RKAP_USER u
ON u.BIRO = mk.BIRO
WHERE mk.ID_KODE = p.KODE_ANGGARAN AND
mk.BIRO = 'BSI'
);
I don't see what RKAP_USER is being used for, but I left it in the query, just in case.
Related
I'm attempting to update the LAST_INSPECTION_FW field for all records in the VEHICLES_FW table with the last JOB_DATE_FW for records with the REASON_CODE_FW = 35. However, what's happening is that once the below code is executed, it's not taking into consideration the WHERE clause. This causes all of the records to update when it should just be updating those with the REASON_CODE_FW = 35.
Is there a way to restructure this code to get it working correctly? Please help, thanks!
UPDATE VEHICLES_FW
SET VEHICLES_FW.LAST_INSPECTION_FW = JOB_HEADERS_FW.FIELD2MAX
FROM VEHICLES_FW
INNER JOIN (SELECT VEHICLE_ID_FW, MAX(JOB_DATE_FW) AS FIELD2MAX
FROM JOB_HEADERS_FW
GROUP BY VEHICLE_ID_FW) AS JOB_HEADERS_FW
ON VEHICLES_FW.VEHICLE_ID_FW = JOB_HEADERS_FW.VEHICLE_ID_FW
INNER JOIN JOB_DETAILS_FW
ON JOB_NUMBER_FW = JOB_NUMBER_FW
WHERE REASON_CODE_FW = '35'
Common Table Expressions are your friend here. SQL Server's strange UPDATE ... FROM syntax is not. EG
with JOB_HEADERS_FW_BY_VEHICLE_ID as
(
SELECT VEHICLE_ID_FW, MAX(JOB_DATE_FW) AS FIELD2MAX
FROM JOB_HEADERS_FW
GROUP BY VEHICLE_ID_FW
), q as
(
Select VEHICLES_FW.LAST_INSPECTION_FW, JOB_HEADERS_FW_BY_VEHICLE_ID.FIELD2MAX NEW_LAST_INSPECTION_FW
FROM VEHICLES_FW
INNER JOIN JOB_HEADERS_FW_BY_VEHICLE_ID
ON VEHICLES_FW.VEHICLE_ID_FW = JOB_HEADERS_FW_BY_VEHICLE_ID.VEHICLE_ID_FW
INNER JOIN JOB_DETAILS_FW
ON JOB_NUMBER_FW = JOB_NUMBER_FW
WHERE REASON_CODE_FW = '35'
)
UPDATE q set LAST_INSPECTION_FW = NEW_LAST_INSPECTION_FW
I suspect this does what you want:
update v
set last_inspection_fw = (
select max(j.job_date_fw)
from job_headers_fw j
inner join job_details_fw jd on jd.job_number_fw = j.job_number_fw
where j.vehicle_id_fw = v.vehicle_id_fw and jd.reason_code_fw = 35
)
from vehicles_fw v
SELECT
Email_address, COUNT(Order_date)
FROM
(SELECT
cust.Email_address, COUNT(ol.Variant_name), ord.Order_date
FROM
DMW_Order_Line_v3 ol
JOIN
DMW_Order_v3 ord ON ol.Unique_transaction_identifier = ord.Unique_transaction_identifier
AND ol.Brand_country = ord.Brand_country
JOIN
DMW_Customer_v3 cust ON ord.Email_address = cust.Email_address
AND ord.Brand_country = cust.Brand_country
WHERE
ord.Brand_country = 'kiehls-emea_CZ'
AND cust.Address_country = 'CZ'
AND cust.Optin_email != 'False'
AND ol.Line_status = 'SHIPPED'
AND ol.Variant_name = 'Sample'
GROUP BY
cust.Email_address, ord.Order_date
HAVING
COUNT(ol.Variant_name) >= 4)
GROUP BY
Email_address
Please, forgive me that I'm posting the whole body of the code. But it might be helpful somehow, who knows. As you can see it is a query on subquery containing joins. I'm using Salesforce SQL. When I run the code, I get this error:
Error saving the query field. Incorrect syntax near the keyword 'GROUP'.
What am I doing wrong? Besides being a noob ;-)
You don't need the count column in the subquery:
SELECT Email_address, COUNT(*)
FROM (SELECT cust.Email_address, ord.Order_date
FROM DMW_Order_Line_v3 ol JOIN
DMW_Order_v3 ord
ON ol.Unique_transaction_identifier = ord.Unique_transaction_identifier AND
ol.Brand_country = ord.Brand_country JOIN
DMW_Customer_v3 cust
ON ord.Email_address = cust.Email_address AND
ord.Brand_country = cust.Brand_country
WHERE ord.Brand_country = 'kiehls-emea_CZ' AND
cust.Address_country = 'CZ' AND
cust.Optin_email <> 'False' AND
ol.Line_status = 'SHIPPED' AND
ol.Variant_name = 'Sample'
GROUP BY cust.Email_address, ord.Order_date
HAVING COUNT(ol.Variant_name) >= 4
) e
GROUP BY Email_Address
I'm attempting to write an SQL statement that use Excel parameters to insert data via a drop down menu. This normally works great, but I think because I'm using a subquery in this case I'm getting the error "The multi-part identifier could not be bound." The subquery is used to create a sum of the quantity of an item purchased within the past week. Here's the code:
SELECT DISTINCT ITMMASTER.ITMREF_0 AS ITEMCODE, ITMMASTER.ITMDES1_0 AS ITEMDESC, Sum(ITMMVT.PHYSTO_0) AS AVAILSTOCK, Sum(ITMMVT.ORDSTO_0) AS ONORDER, Sum(ITMMVT.PHYALL_0) AS ALLOCATED, Sum(ITMFACILIT.MAXSTO_0) AS MAX,
INVQTY = (SELECT Sum(SINVOICED.QTY_0) AS INVQTY
FROM x3v11.PROD.SINVOICED SINVOICED
WHERE SINVOICED.INVDAT_0 BETWEEN DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0))
AND DATEADD(DAY,4,DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0)))
AND SINVOICED.ITMREF_0 = ITMMASTER.ITMREF_0
),
ITMMASTER.ITMWEI_0 AS WEIGHT, ITMCOST.CSTTOT_0 AS COST, ITMBPS.BPSNUM_0 AS VENDOR, BPSUPPLIER.BPSNAM_0 AS VENDNAME, ITMMASTER.TSICOD_2 AS CAT
FROM x3v11.PROD.ITMMASTER ITMMASTER
LEFT OUTER JOIN x3v11.PROD.ITMMVT ITMMVT ON ITMMASTER.ITMREF_0 = ITMMVT.ITMREF_0
LEFT OUTER JOIN x3v11.PROD.ITMFACILIT ITMFACILIT ON ITMMVT.ITMREF_0 = ITMFACILIT.ITMREF_0 AND ITMMVT.STOFCY_0 = ITMFACILIT.STOFCY_0
LEFT OUTER JOIN x3v11.PROD.ITMCOST ITMCOST ON ITMMVT.ITMREF_0 = ITMCOST.ITMREF_0 AND ITMMVT.STOFCY_0 = ITMCOST.STOFCY_0
LEFT OUTER JOIN x3v11.PROD.ITMBPS ITMBPS ON ITMMASTER.ITMREF_0 = ITMBPS.ITMREF_0
LEFT OUTER JOIN x3v11.PROD.BPSUPPLIER BPSUPPLIER ON ITMBPS.BPSNUM_0 = BPSUPPLIER.BPSNUM_0
WHERE ITMCOST.STOFCY_0 <> '115'
AND ITMFACILIT.MAXSTO_0 <> 0
AND ITMBPS.PIO_0 <> 99
AND ITMBPS.BPSNUM_0 LIKE CASE WHEN ? IS NOT NULL THEN ? ELSE '%' END
GROUP BY ITMMASTER.ITMREF_0, ITMMASTER.ITMDES1_0, ITMMASTER.ITMWEI_0, ITMCOST.CSTTOT_0, ITMBPS.BPSNUM_0, BPSUPPLIER.BPSNAM_0, ITMMASTER.TSICOD_2
ORDER BY ITMMASTER.ITMREF_0
I think you have specified the syntax for the subquery incorrectly:
INVQTY = (SELECT Sum(SINVOICED.QTY_0) AS INVQTY
FROM x3v11.PROD.SINVOICED SINVOICED
WHERE SINVOICED.INVDAT_0 BETWEEN
DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0))
AND DATEADD(DAY,4,DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0)))
AND SINVOICED.ITMREF_0 = ITMMASTER.ITMREF_0
),
I think it should be:
(SELECT Sum(SINVOICED.QTY_0)
FROM x3v11.PROD.SINVOICED SINVOICED
WHERE SINVOICED.INVDAT_0 BETWEEN
DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0))
AND DATEADD(DAY,4,DATEADD(WEEK,-1,DATEADD(WEEK,DATEDIFF(WEEK,0,GETDATE()),0)))
AND SINVOICED.ITMREF_0 = ITMMASTER.ITMREF_0
) AS INVQTY,
So I have just upgraded to MySQL 8 to test if it would be a viable option, but I have stumbled upon quiet the riddle.
It throws an error at 'table_product_features AS pf' but I can't seem to figure out why as in the documentation it is done exactly the same way: https://dev.mysql.com/doc/refman/8.0/en/join.html
Error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups ON pf.parent_id = groups.feature_id LEFT JOIN table_product_features_des' at line 1 (1064)
I also checked the bug tracker but that doesnt seem to have anything related to my issue.
What exactly am I doing wrong here?
P.S. I put it in a code snippet so that it would remain formatted.
SELECT pf.feature_id,
pf.company_id,
pf.feature_type,
pf.parent_id,
pf.display_on_product,
pf.display_on_catalog,
pf.display_on_header,
table_product_features_descriptions.description,
table_product_features_descriptions.lang_code,
table_product_features_descriptions.prefix,
table_product_features_descriptions.suffix,
pf.categories_path,
table_product_features_descriptions.full_description,
pf.status,
pf.comparison,
pf.position,
groups.position AS group_position,
table_product_features_values.value,
table_product_features_values.variant_id,
table_product_features_values.value_int
FROM table_product_features AS pf
LEFT JOIN table_product_features AS groups
ON pf.parent_id = groups.feature_id
LEFT JOIN table_product_features_descriptions
ON table_product_features_descriptions.feature_id = pf.feature_id
AND table_product_features_descriptions.lang_code = 'en'
INNER JOIN table_product_features_values
ON table_product_features_values.feature_id = pf.feature_id
AND table_product_features_values.product_id = 230
AND table_product_features_values.lang_code = 'en'
INNER JOIN table_ult_objects_sharing
ON ( table_ult_objects_sharing.share_object_id = pf.feature_id
AND table_ult_objects_sharing.share_company_id = 1
AND table_ult_objects_sharing.share_object_type =
'product_features' )
WHERE 1
AND pf.feature_type != 'G'
AND pf.status IN ( 'A' )
AND pf.display_on_product = 'Y'
AND ( pf.categories_path = ''
OR Isnull(pf.categories_path)
OR Find_in_set(203, pf.categories_path)
OR Find_in_set(215, pf.categories_path)
OR Find_in_set(216, pf.categories_path) )
GROUP BY pf.feature_id
ORDER BY group_position,
pf.position,
table_product_features_descriptions.description
If your not using aggregate functions I don't see why you need the GROUP BY.
Also groups is a reserved word, change it to tgroups or something else.
SELECT pf.feature_id,
pf.company_id,
pf.feature_type,
pf.parent_id,
pf.display_on_product,
pf.display_on_catalog,
pf.display_on_header,
tdesc.description,
tdesc.lang_code,
tdesc.prefix,
tdesc.suffix,
pf.categories_path,
tdesc.full_description,
pf.status,
pf.comparison,
pf.position,
tgroups.position group_position,
tvals.value,
tvals.variant_id,
tvals.value_int
FROM table_product_features pf
LEFT JOIN table_product_features tgroups ON pf.parent_id = tgroups.feature_id
LEFT JOIN table_product_features_descriptions tdesc ON tdesc.feature_id = pf.feature_id AND tdesc.lang_code = 'en'
INNER JOIN table_product_features_values tvals ON tvals.feature_id = pf.feature_id AND tvals.product_id = 230 AND tvals.lang_code = 'en'
INNER JOIN table_ult_objects_sharing tshar ON (tshar.share_object_id = pf.feature_id AND tshar.share_company_id = 1 AND tshar.share_object_type = 'product_features')
WHERE 1
AND pf.feature_type != 'G'
AND pf.status IN ('A')
AND pf.display_on_product = 'Y'
AND (pf.categories_path = '' OR Isnull(pf.categories_path) OR Find_in_set(203, pf.categories_path) OR Find_in_set(215, pf.categories_path) OR Find_in_set(216, pf.categories_path))
ORDER BY group_position, pf.position, tdesc.description
I need to update the column of a temp table from the count of a field from another table. I was trying to do this with the following query, but I get a syntax error.
How should I write this query?
Thanks!
UPDATE #ResultadosTest
SET PalletsReservados = COUNT(pe.paen_numero)
FROM #ResultadosTest RT, dba.spro_palletencab pe
WHERE pe.pate_tempor = RT.pate_tempor
AND pe.expo_codigo = RT.expo_codigo
AND pe.WeekLinId = RT.WeekLinId
AND pe.plde_codigo = RT.plde_codigo
AND pe.paen_estado = 1
AND ISNULL(pe.LoteCargaId, 0) <> 0
AND ISNULL(pe.PREMOPID, 0) <> 0
GROUP BY
pe.pate_tempor, pe.expo_codigo, pe.WeekLinId, pe.plde_codigo
The error is:
Line 10, Incorrect syntax near the keyword 'GROUP' (42000,156)
You can't directly use an aggregate function at update statement. Instead you can you a derived table
Update #ResultadosTest
SET PalletsReservados = ppp.countpaen_numero
From (Select Count(pe.paen_numero) as countpaen_numero ,pe.pate_tempor,spro_palletencab,........
FROM #ResultadosTest RT,dba.spro_palletencab pe
Where pe.pate_tempor=RT.pate_tempor
AND pe.expo_codigo=RT.expo_codigo
AND pe.WeekLinId=RT.WeekLinId
AND pe.plde_codigo=RT.plde_codigo
AND pe.paen_estado=1 AND IsNull(pe.LoteCargaId,0)<>0 AND IsNull(pe.PREMOPID,0)<>0
GROUP BY pe.pate_tempor,pe.expo_codigo,pe.WeekLinId,pe.plde_codigo ) ppp
Where ppp.pate_tempor = Field..
First group, then join
Update #ResultadosTest
SET PalletsReservados = pe.Cnt
FROM #ResultadosTest RT
JOIN (
SELECT pate_tempor, expo_codigo, WeekLinId, plde_codigo, Count(pe.paen_numero) cnt
FROM dba.spro_palletencab pe
WHERE paen_estado=1 AND IsNull(LoteCargaId,0)<>0 AND IsNull(PREMOPID,0)<>0
GROUP BY pate_tempor,expo_codigo,WeekLinId,plde_codigo
) pe ON pe.pate_tempor=RT.pate_tempor
AND pe.expo_codigo=RT.expo_codigo
AND pe.WeekLinId=RT.WeekLinId
AND pe.plde_codigo=RT.plde_codigo;
should work like:
UPDATE RT
SET PalletsReservados = pe.ctr
FROM #ResultadosTest RT
INNER JOIN
(
SELECT pate_tempor
,expo_codigo
,WeekLinId
,plde_codigo
,Count(pe.paen_numero) as ctr
FROM dba.spro_palletencab
WHERE paen_estado = 1
AND ISNULL(LoteCargaId,0) <> 0
AND ISNULL(PREMOPID,0) <> 0
GROUP BY pe.pate_tempor
,pe.expo_codigo
,pe.WeekLinId
,pe.plde_codigo
) pe
ON pe.pate_tempor = RT.pate_tempor
AND pe.expo_codigo = RT.expo_codigo
AND pe.WeekLinId = RT.WeekLinId
AND pe.plde_codigo = RT.plde_codigo