POSTGRES Update with left outer join is not working - sql

Please suggest, what am I doing wrong.
UPDATE uc
SET uc.selected_value_id = cv.id, uc.fixed_value = NULL
FROM unit_characteristic uc
left JOIN characteristic_value cv ON uc.fixed_value like CONCAT(cv.value,'%')
WHERE cv.characteristic_id = 6
and uc.characteristic_id = 6
and uc.unit_id in (6313,6314)
Getting error
SQL Error [42P01]: ERROR: relation "uc" does not exist
Position: 8
org.postgresql.util.PSQLException: ERROR: relation "uc" does not exist
Position: 8
While this select is working fine
select count(uc.*)
FROM unit_characteristic uc
left JOIN characteristic_value cv ON uc.fixed_value like CONCAT(cv.value,'%')
WHERE cv.characteristic_id = 6
and uc.characteristic_id = 6
and uc.unit_id in (6313,6314)

you don't have to repeat the target table in the FROM clause; it is already in the range table
the target table can have an alias
but, the SET columnname = new_value line must not use this alias. It is implicit (since there is only one table reference to be updated)
UPDATE unit_characteristic uc
SET selected_value_id = cv.id
, fixed_value = NULL
FROM characteristic_value cv
WHERE uc.fixed_value like cv.value || '%'
AND cv.characteristic_id = 6
AND uc.characteristic_id = 6
AND uc.unit_id in (6313, 6314)
;

In Postgres, the reference in the update cannot refer to a table in the from. I suspect you want:
update unit_characteristic uc
set selected_value_id = cv.id,
fixed_value = NULL
from characteristic_value cv
where uc.fixed_value like cv.value || '%' and
cv.characteristic_id = 6 and
uc.characteristic_id = 6 and
uc.unit_id in (6313, 6314);
Note that your version of the query uses left join. But the where clause turns that into an inner join anway.

Related

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!!

SQL query fails, no idea why

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

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'

update with subquery

The code :
UPDATE tt_t_documents
SET t_Doc_header_ID = (SELECT
MIN(dh.Doc_header_ID)
FROM tt_t_documents td WITH (NOLOCK)
JOIN Doc_header dh WITH (NOLOCK)
ON dh.DH_doc_number = td.t_dh_doc_number
AND dh.DH_sub = 1
JOIN Pred_entry pe WITH (NOLOCK)
ON pe.Pred_entry_ID = dh.DH_pred_entry
JOIN Doc_type dt WITH (NOLOCK)
ON dty.Doc_type_ID = pe.PD_doc_type
AND dt.DT_mode = 5
HAVING COUNT(dh.Doc_header_ID) = 1);
I want to update my columns, but before that I also want to check if there is only one ID found.
The problem in this select is that I get more than one ID.
How can I write a query that updates each row and checks in the same query that there is only one id found?
I am guessing that you intend something like this:
update td
set t_Doc_header_ID = min_Doc_header_ID
from tt_t_documents td join
(select DH_doc_number, min(dh.Doc_header_ID) as min_Doc_header_ID
from Doc_header dh join
Pred_entry pe
on pe.Pred_entry_ID = dh.DH_pred_entry join
Doc_type dt
on dty.Doc_type_ID = pe.PD_doc_type and dt.DT_mode = 5
where dh.DH_doc_number = td.t_dh_doc_number and dh.DH_sub = 1
group by DH_doc_number
having count(dh.Doc_header_ID) = 1
) dh
on dh.DH_doc_number = td.t_dh_doc_number;
Using a join also means that you do not update the values where the condition does not match. If you use a left join, then the values will be updated to NULL (if that is your intention).
I'm not sure I believe you are getting more than one id back with that select given that you are doing a 'min' on it and have no group by. It should be only returning the lowest value for Doc_header_id.
To do what you are asking, first, you should have some way of joining to the tt_t_documents table in the update statement (eg. where td.id == tt_t_documents.id).
Second, you could re-write it to use the sub-query in the from. Something like:
update
tt_t_documents
set
t_Doc_header_ID = x.Doc_Header_id
from tt_t_documents join (
select td.id,
min(dh.Doc_header_ID)
from
tt_t_documents td
join Doc_header dh
on dh.DH_doc_number = td.t_dh_doc_number
and dh.DH_sub = 1
join Pred_entry pe
on pe.Pred_entry_ID = dh.DH_pred_entry
join Doc_type dt
on dty.Doc_type_ID = pe.PD_doc_type
and dt.DT_mode = 5
group by td.id
having
count(dh.Doc_header_ID) = 1
) x on tt_t_documents.id= x.id;
The syntax may not be perfect and I'm not sure how you want to find the doc_header_id but it would be something like this. The sub query would only return values with 1 doc_header_id). Not knowing the schema of your tables, this is as close as I can get.

Oracle 9i SQL query - join multiple time the same table

I've a strange behavior with a simple SQL query. I work on Oracle 9i.
I just wanna be sure there is no stupid error in my query. If anyone can tell me what I'm doing wrong. Here's the query :
select p.PARTNUM,
p.N_DATAA, dataa1.ID_DATA as ID_DATAA,
p.N_DATAF, dataf1.ID_DATA as ID_DATAF,
p.N_DATAS, datas1.ID_DATA as ID_DATAS
from PIECES p
left join DATA dataa1 on
dataa1.N_DATA = p.N_DATAA AND
dataa1.REV_DATA = ( select max(dataa2.REV_DATA) from DATA dataa2
where dataa2.N_DATA = p.N_DATAA )
left join DATA dataf1 on
dataf1.N_DATA = p.N_DATAF AND
dataf1.REV_DATA = ( select max(dataf2.REV_DATA) from DATA dataf2
where dataf2.N_DATA = p.N_DATAF )
left join DATA datas1 on
datas1.N_DATA = p.N_DATAS AND
datas1.REV_DATA = ( select max(datas2.REV_DATA) from DATA datas2
where datas2.N_DATA = p.N_DATAS )
where p.PARTNUM='MYPARTNUM';
and here is the result :
"PARTNUM" "N_DATAA" "ID_DATAA" "N_DATAF" "ID_DATAF" "N_DATAS" "ID_DATAS"
"MYPARTNUM" "A23240" "300" "F4130" "398" "S2330" ""
My problem appears on the field ID_DATAS (an ID_DATAS should be returned, I'm sure, see below), it acts as if it ignores the last join and retrieve no data. If I delete the SECOND join, it works perfectly. It seems like if Oracle does not support more than 2 left join... (???)
Here an other example when I switch the two last left join (in this case the problem appears on ID_DATAF) :
select p.PARTNUM,
p.N_DATAA, dataa1.ID_DATA as ID_DATAA,
p.N_DATAF, dataf1.ID_DATA as ID_DATAF,
p.N_DATAS, datas1.ID_DATA as ID_DATAS
from PIECES p
left join DATA dataa1 on
dataa1.N_DATA = p.N_DATAA AND
dataa1.REV_DATA = ( select max(dataa2.REV_DATA) from DATA dataa2
where dataa2.N_DATA = p.N_DATAA )
left join DATA datas1 on
datas1.N_DATA = p.N_DATAS AND
datas1.REV_DATA = ( select max(datas2.REV_DATA) from DATA datas2
where datas2.N_DATA = p.N_DATAS )
left join DATA dataf1 on
dataf1.N_DATA = p.N_DATAF AND
dataf1.REV_DATA = ( select max(dataf2.REV_DATA) from DATA dataf2
where dataf2.N_DATA = p.N_DATAF )
where p.PARTNUM='MYPARTNUM';
and here is the result :
"PARTNUM" "N_DATAA" "ID_DATAA" "N_DATAF" "ID_DATAF" "N_DATAS" "ID_DATAS"
"MYPARTNUM" "A23240" "300" "F4130" "" "S2330" "400"
Thank you for your help :)
EDIT : add fully qualified column names in queries (does not solve my problem)
You are probably experiencing a bug with your old version of Oracle. Oracle 9i was the first version to support ANSI join and came with a few bugs with ANSI-join syntax.
Your query won't run in an up-to-date (11.2) Oracle db (SQLFiddle), you will run into:
ORA-01799: a column may not be outer-joined to a subquery
So the bug in your Oracle version is that this query shouldn't return any result. In any case make sure that you're running the latest patch set. Also it may be a good idea to plan an upgrade, this version is deprecated -- extended support ended 3 years ago!
You'll have to rewrite your query, for instance like this:
SQL> WITH filtered_data AS (
2 SELECT n_data, rev_data, id_data
3 FROM DATA d
4 WHERE rev_data = (SELECT MAX(rev_data)
5 FROM DATA d_in
6 WHERE d_in.n_data = d.n_data)
7 )
8 SELECT p.partnum,
9 p.n_dataa,
10 A.id_data AS id_dataa,
11 p.n_dataf,
12 F.id_data AS id_dataf,
13 p.n_datas,
14 S.id_data AS id_datas
15 FROM pieces p
16 LEFT JOIN filtered_data A ON A.n_data = p.n_dataa
17 LEFT JOIN filtered_data S ON S.n_data = p.n_datas
18 LEFT JOIN filtered_data F ON F.n_data = p.n_dataf
19 WHERE p.PARTNUM = 'MYPARTNUM';
PARTNUM N_DATAA ID_DATAA N_DATAF ID_DATAF N_DATAS ID_DATAS
------------- -------- --------- -------- --------- -------- ---------
MYPARTNUM A23240 300 F4130 398 S2330 400