SQL query fails, no idea why - sql

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

Related

UPDATE statement with JOIN in SQL Server Not Working as Expected

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

SQL query case when then in where clause

I read a lot of similar questions but didn't find a solution for me. Basically I would like to have a where clause (AND PEKP.VORGANGS_ART = 'BE') only if a special condition (PFSP.EINKAUFS_KZ = 2) is true.
I tried in many ways like that:
SELECT *
FROM PFSP
LEFT OUTER JOIN PFAK on PFSP.RUECKMELDE_NR = PFAK.RUECKMELDE_NR
LEFT OUTER JOIN PEKP ON (PFSP.BESTELL_NR=PEKP.VORGANGS_NR)
AND (PFSP.BESTELL_POS_NR=PEKP.VORGANGS_POS_NR)
LEFT OUTER JOIN PMLB ON PFSP.KOMPONENTEN_ARTIKEL_NR=PMLB.ARTIKEL_NR
WHERE PFAK.KD_VORGANGS_NR = '910-001213'
AND PFSP.RUECKMELDE_STATUS = '3'
AND PFSP.BESCHAFFUNGSKENNER = 'F'
AND CASE PFSP.EINKAUFS_KZ
WHEN 2 THEN PEKP.VORGANGS_ART = 'BE'
END
but I keep getting errors:
wrong syntax near '='"
Skip the CASE, use AND/OR instead:
AND (PFSP.EINKAUFS_KZ <> 2 OR PEKP.VORGANGS_ART = 'BE')
If PFSP.EINKAUFS_KZ = 2, then PEKP.VORGANGS_ART must be equal to 'BE'.
If PFSP.EINKAUFS_KZ <> 2, it doesn't matter what PEKP.VORGANGS_ART is.
With this CASE expression:
AND 1 = CASE
WHEN PFSP.EINKAUFS_KZ = 2 AND PEKP.VORGANGS_ART = 'BE' THEN 1
WHEN PFSP.EINKAUFS_KZ = 2 THEN 0
ELSE 1
END
The order of the conditions is important.
You probably want something like:
AND (
(PFSP.EINKAUFS_KZ = 2 AND PEKP.VORGANGS_ART='BE')
OR PFSP.EINKAUFS_KZ <> 2 --You may need to catch NULL as well
)
SELECT *
FROM PFSP
LEFT OUTER JOIN PFAK on PFSP.RUECKMELDE_NR = PFAK.RUECKMELDE_NR
LEFT OUTER JOIN PEKP ON (PFSP.BESTELL_NR=PEKP.VORGANGS_NR)
AND (PFSP.BESTELL_POS_NR=PEKP.VORGANGS_POS_NR)
LEFT OUTER JOIN PMLB ON PFSP.KOMPONENTEN_ARTIKEL_NR=PMLB.ARTIKEL_NR
WHERE PFAK.KD_VORGANGS_NR = '910-001213'
AND PFSP.RUECKMELDE_STATUS = '3'
AND PFSP.BESCHAFFUNGSKENNER = 'F'
AND ((PFSP.EINKAUFS_KZ = 2 AND PEKP.VORGANGS_ART = 'BE') OR (PFSP.EINKAUFS_KZ <> 2) )

ORA-00971: Missing SET keyword

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.

Unsure why ORA-00918 column ambiguously defined is appearing

I'm unsure as to why I'm getting the ORA-00918 error message appearing when I type in the following code.
I can't see which column is ambiguously defined.
What I want to do is create a table that pulls in the b.site_code value based on the work_header_no, work_version_no and site_numbers matching in queries A & B matching.
Code is below
SELECT
a.statement.statement_date,
a.sw_header.organise_code,
a.organisation.organise_name,
a.PermitRef,
a.actual_inspection.logged_time,
a.insp_category.insp_category_name,
a.actual_inspection.insp_number,
a.actual_inspection.site_number,
a.inspection_outcome.insp_outcome_name,
a.insp_category.insp_charge,
a.actual_inspection.insp_notes,
a.actual_inspection.work_header_no,
a.actual_inspection.insp_time,
b.site_code
FROM
(select
statement.statement_date,
sw_header.organise_code,
organisation.organise_name,
CAST(
organisation.external_ref_2 ||''||
sw_header.works_ref||'.'||
sw_notice_header.app_seq_no||'.'||
sw_notice_header.ext_version_no
as VARCHAR (40)) as PermitRef,
actual_inspection.logged_time,
insp_category.insp_category_name,
actual_inspection.insp_number,
actual_inspection.site_number,
inspection_outcome.insp_outcome_name,
insp_category.insp_charge,
actual_inspection.insp_notes,
actual_inspection.work_header_no,
actual_inspection.insp_time,
sw_notice_header.work_header_no,
sw_notice_header.work_version_no,
actual_inspection.site_number
from
actual_inspection
inner join sw_header on
actual_inspection.work_header_no = sw_header.work_header_no
inner join sw_notice_header on
sw_header.work_header_no = sw_notice_header.work_header_no
and sw_header.work_version_no = sw_notice_header.work_version_no
inner join insp_category on
actual_inspection.insp_category_code = insp_category.insp_category_code
inner join inspection_outcome on
actual_inspection.insp_outcome_code = inspection_outcome.insp_outcome_code
inner join organisation on
sw_header.organise_code = organisation.organise_code
inner join statement on
organisation.organise_code = statement.organise_code
and organisation.statement_number = statement.statement_no
where
actual_inspection.notice_type_code = '2600' and
actual_inspection.insp_outcome_code != 'O40'
order by
actual_inspection.logged_time)
a
JOIN
(
select
sns.work_header_no,
sns.work_version_no,
sns.site_number,
sns.site_code
from
sw_notice_site sns
)
b
ON a.work_header_no = b.work_header_no and
a.work_version_no = b.work_version_no and
a.site_number = b.site_number
You have duplicate actual_inspection.site_number and work_header_no remove the duplicate rows
actual_inspection.site_number,
inspection_outcome.insp_outcome_name,
insp_category.insp_charge,
actual_inspection.insp_notes,
actual_inspection.work_header_no,
actual_inspection.insp_time,
sw_notice_header.work_header_no,
sw_notice_header.work_version_no,
actual_inspection.site_number
No need to use a.statement.statement_date. You can use a.statement_date.
Likewise change all other columns for a..
Your whole query should look like this:
SELECT -- removed table names from all the columns
A.STATEMENT_DATE,
A.ORGANISE_CODE,
A.ORGANISE_NAME,
A.PERMITREF,
A.LOGGED_TIME,
A.INSP_CATEGORY_NAME,
A.INSP_NUMBER,
A.SITE_NUMBER,
A.INSP_OUTCOME_NAME,
A.INSP_CHARGE,
A.INSP_NOTES,
A.WORK_HEADER_NO,
A.INSP_TIME,
B.SITE_CODE
FROM
(
SELECT
STATEMENT.STATEMENT_DATE,
SW_HEADER.ORGANISE_CODE,
ORGANISATION.ORGANISE_NAME,
CAST(ORGANISATION.EXTERNAL_REF_2
|| ''
|| SW_HEADER.WORKS_REF
|| '.'
|| SW_NOTICE_HEADER.APP_SEQ_NO
|| '.'
|| SW_NOTICE_HEADER.EXT_VERSION_NO AS VARCHAR(40)) AS PERMITREF,
ACTUAL_INSPECTION.LOGGED_TIME,
INSP_CATEGORY.INSP_CATEGORY_NAME,
ACTUAL_INSPECTION.INSP_NUMBER,
--ACTUAL_INSPECTION.SITE_NUMBER, -- commented this as it is there in statement twice
INSPECTION_OUTCOME.INSP_OUTCOME_NAME,
INSP_CATEGORY.INSP_CHARGE,
ACTUAL_INSPECTION.INSP_NOTES,
ACTUAL_INSPECTION.WORK_HEADER_NO,
ACTUAL_INSPECTION.INSP_TIME,
--SW_NOTICE_HEADER.WORK_HEADER_NO, -- commented this as it is there in statement twice
SW_NOTICE_HEADER.WORK_VERSION_NO,
ACTUAL_INSPECTION.SITE_NUMBER
FROM
ACTUAL_INSPECTION
INNER JOIN SW_HEADER ON ACTUAL_INSPECTION.WORK_HEADER_NO = SW_HEADER.WORK_HEADER_NO
INNER JOIN SW_NOTICE_HEADER ON SW_HEADER.WORK_HEADER_NO = SW_NOTICE_HEADER.WORK_HEADER_NO
AND SW_HEADER.WORK_VERSION_NO = SW_NOTICE_HEADER.WORK_VERSION_NO
INNER JOIN INSP_CATEGORY ON ACTUAL_INSPECTION.INSP_CATEGORY_CODE = INSP_CATEGORY.INSP_CATEGORY_CODE
INNER JOIN INSPECTION_OUTCOME ON ACTUAL_INSPECTION.INSP_OUTCOME_CODE = INSPECTION_OUTCOME.INSP_OUTCOME_CODE
INNER JOIN ORGANISATION ON SW_HEADER.ORGANISE_CODE = ORGANISATION.ORGANISE_CODE
INNER JOIN STATEMENT ON ORGANISATION.ORGANISE_CODE = STATEMENT.ORGANISE_CODE
AND ORGANISATION.STATEMENT_NUMBER = STATEMENT.STATEMENT_NO
WHERE
ACTUAL_INSPECTION.NOTICE_TYPE_CODE = '2600'
AND ACTUAL_INSPECTION.INSP_OUTCOME_CODE != 'O40'
ORDER BY
ACTUAL_INSPECTION.LOGGED_TIME
) A
JOIN (
SELECT
SNS.WORK_HEADER_NO,
SNS.WORK_VERSION_NO,
SNS.SITE_NUMBER,
SNS.SITE_CODE
FROM
SW_NOTICE_SITE SNS
) B ON A.WORK_HEADER_NO = B.WORK_HEADER_NO
AND A.WORK_VERSION_NO = B.WORK_VERSION_NO
AND A.SITE_NUMBER = B.SITE_NUMBER;
Cheers!!

Invalid Identifier in Temp Table Left Join Clause

Good Evening EST,
I am stuck on the below query, I keep getting an invalid identifier error at line 12, the first "DELETE FLAG". Not sure if I'm missing some syntax or doing something else silly. (I have very thoroughly checked I didn't misspell anything and all the fields exist)
Oracle SQL Developer V. 17.2 Any help would be greatly appreciated.
SELECT
CM.SUBJECT_PERSON_ID,
CM.COHORT_SCOPED_IDENTIFIER AS SUBJECT_STUDY_ID,
SA3.ALT_ID_VALUE AS GLOBAL_SUBJECT_ID,
V.PREFERRED_TERM AS MEMBER_STATUS,
CM.COHORT_ID,
SA2.ALT_ID_VALUE AS CRIN_ID
FROM GLENAPO.COHORT_MEMBER CM
LEFT JOIN GLENAPO.SUBJECT_ALT_ID SA1
ON SA2.SUBJECT_PERSON_ID =CM.SUBJECT_PERSON_ID
AND SA2.IDENTIFIER_TYPE_ID = 8 -- get SDG_ID
AND SA2.DELETE_FLAG = 'N'
LEFT JOIN GLENAPO.SUBJECT_ALT_ID SA1
ON SA2.SUBJECT_PERSON_ID =CM.SUBJECT_PERSON_ID
AND SA2.IDENTIFIER_TYPE_ID = 8 -- get SDG_ID
AND SA2.DELETE_FLAG = 'N'
LEFT JOIN GLENAPO.SUBJECT_ALT_ID SA2
ON SA2.SUBJECT_PERSON_ID =CM.SUBJECT_PERSON_ID
AND SA2.IDENTIFIER_TYPE_ID = 18 -- get CRIN ID
AND SA2.DELETE_FLAG = 'N'
JOIN GLENAPO.SUBJECT_ALT_ID SA3
ON SA3.SUBJECT_PERSON_ID =CM.SUBJECT_PERSON_ID
AND SA3.IDENTIFIER_TYPE_ID = 12 -- get Global Subject
AND SA3.DELETE_FLAG = 'N'
LEFT JOIN GLENAPO.VOCAB_TERM_VIEW V
ON V.TABLE_NAME = 'COHORT_MEMBER'
AND V.COLUMN_NAME = 'STATUS_CODE'
AND V.CONCEPT_ID = CM.STATUS_CODE
LEFT JOIN GLENAPO.FACILITY_REFERENCE FR ON FR.FACILITY_ID = CM.FACILITY_ID
WHERE CM.DELETE_FLAG = 'N'
This is the beginning of your FROM clause:
FROM GLENAPO.COHORT_MEMBER CM LEFT JOIN
GLENAPO.SUBJECT_ALT_ID SA1
ON SA2.SUBJECT_PERSON_ID = CM.SUBJECT_PERSON_ID AND
SA2.IDENTIFIER_TYPE_ID = 8 AND -- get SDG_ID
SA2.DELETE_FLAG = 'N'
A table alias needs to be defined before it is used. I think you intend SA1, rather than SA2:
FROM GLENAPO.COHORT_MEMBER CM LEFT JOIN
GLENAPO.SUBJECT_ALT_ID SA1
ON SA1.SUBJECT_PERSON_ID = CM.SUBJECT_PERSON_ID AND
SA1.IDENTIFIER_TYPE_ID = 8 AND -- get SDG_ID
SA1.DELETE_FLAG = 'N'