问题
This seems simple but I can't seem to wrap my head around it... I am trying to update a table, set the columns equal to the result of a query which uses the ID column from the table which I am updating and uses a WITH() clause. I also only want to update the columns in my table where Fall_Term = 'Fall 2019'
I will provide my pseudo code and my long nasty actual code...
NOTE: My update statement works perfectly fine if I manually enter values for the ID column - PERSON_SKEY. For example, instead of:
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY
Doing this works:
WHERE V.PERSON_SKEY = 12345
My code currently gets the error:
[Error] Execution (10: 27): ORA-00904: "SNP_FACULTY_CENSUS"."PERSON_SKEY": invalid identifier
Here is my pseudo code..
update Table_A
set Column_A =
(select Column_B from Table_B
where TABLE_A.id = TABLE_B.id )
where Fall_Term = 'Fall 2019';
Here is my actual code:
--UPDATE OIR TERMINAL DEGREE IN FALL 2019 SNAPSHOT
UPDATE SNP_FACULTY_CENSUS
SET OIR_TERMINAL_DEGREE = (
WITH HIGHES_DEG_VALUE AS (
SELECT max(D.OIR_DEGREE_HIERARCHY) AS MAX_DEG
FROM VPAA_PPAGGENL_FIS V
JOIN DIM_DEGREE_CROSSWALK D
ON V.VPAA_FACULTY_DEGREE_CODE =
D.VPAA_DEGREE_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY)
SELECT
CASE
WHEN MAX_DEG = 30
THEN --CHECK IF EXIST ON CROSSWALKS AND USE THAT VALUE IF IT DOES
(CASE
WHEN EXISTS (SELECT V.VPAA_FACULTY_DEGREE_CODE, --IF ON SUBJ CODE CROSSWALK
V.VPAA_FACULTY_SUBJECT_CODE
FROM VPAA_PPAGGENL_FIS V
JOIN DIM_DEGREE_CROSSWALK D
ON V.VPAA_FACULTY_DEGREE_CODE = D.VPAA_DEGREE_CODE
INNER JOIN SUBJ_CODE_CROSSWALK S
ON V.VPAA_FACULTY_DEGREE_CODE = S.VPAA_FACULTY_DEGREE_CODE
and S.VPAA_FACULTY_SUBJECT_CODE = V.VPAA_FACULTY_SUBJECT_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY
)
THEN (SELECT DISTINCT -- USE TERMINAL VALUE FROM SUBJ CODE
CASE
WHEN s.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN s.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM SUBJ_CODE_CROSSWALK S
INNER JOIN VPAA_PPAGGENL_FIS V
ON V.VPAA_FACULTY_DEGREE_CODE =
S.VPAA_FACULTY_DEGREE_CODE
AND S.VPAA_FACULTY_SUBJECT_CODE =
V.VPAA_FACULTY_SUBJECT_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY )
WHEN EXISTS (SELECT DISTINCT V.VPAA_FACULTY_DEGREE_CODE, --IF ON CLIC INSTRUCTORS CROSSWALK
V.VPAA_FACULTY_SUBJECT_CODE,
I.OIR_EMPLOYEE_DEPARTMENT_HOME_ORG
FROM VPAA_PPAGGENL_FIS V
INNER JOIN CLIC_INSTRUCTORS_CROSSWALK C
ON V.VPAA_FACULTY_DEGREE_CODE = C.VPAA_FACULTY_DEGREE_CODE
AND V.VPAA_FACULTY_SUBJECT_CODE = C.VPAA_FACULTY_SUBJECT_CODE
INNER JOIN IPEDS_REPORT_DETAILS I ON V.PIDM = I.HR_PIDM
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY
AND (I.OIR_EMPLOYEE_DEPARTMENT_HOME_ORG = '396000'
OR I.OIR_EMPLOYEE_DEPARTMENT_HOME_ORG = '396010')
)
THEN (SELECT DISTINCT
CASE
WHEN C.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN C.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM CLIC_INSTRUCTORS_CROSSWALK C
INNER JOIN VPAA_PPAGGENL_FIS V
ON C.VPAA_FACULTY_DEGREE_CODE =
V.VPAA_FACULTY_DEGREE_CODE
AND C.VPAA_FACULTY_SUBJECT_CODE =
V.VPAA_FACULTY_SUBJECT_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY ))
ELSE --USE DIM DEGREE CROSSWALK NORMALLY
(SELECT DISTINCT
CASE
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM DIM_DEGREE_CROSSWALK D
JOIN VPAA_PPAGGENL_FIS V
ON V.VPAA_FACULTY_DEGREE_CODE =
D.VPAA_DEGREE_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY)
ORDER BY OIR_DEGREE_HIERARCHY DESC
FETCH FIRST ROW ONLY )
END) --USE DIM DEGREE CROSSWALK FOR THE TERMINAL VALUE FOR THE MAX DEG THERE
WHEN MAX_DEG != 30
THEN
(SELECT DISTINCT
CASE
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM DIM_DEGREE_CROSSWALK D
JOIN VPAA_PPAGGENL_FIS V
ON V.VPAA_FACULTY_DEGREE_CODE =
D.VPAA_DEGREE_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY
ORDER BY OIR_DEGREE_HIERARCHY DESC
FETCH FIRST ROW ONLY )
ELSE NULL
END
FROM HIGHES_DEG_VALUE )
WHERE SNP_FACULTY_CENSUS.OIR_FALL_TERM = 'Fall 2019'
;
Structure of tables:
CREATE TABLE SNP_FACULTY_CENSUS --(Snapshot table)
(
OIR_ACADEMIC_RANK VARCHAR2(50 BYTE),
OIR_DEPARTMENT_HOME_ORG VARCHAR2(50 BYTE),
OIR_EMPLOYEE_NAME_LAST_FIRST VARCHAR2(300 BYTE),
HR_POSN_ESOC VARCHAR2(10 BYTE),
OIR_FT_PT VARCHAR2(25 BYTE),
OIR_FACULTY_STATUS VARCHAR2(25 BYTE),
OIR_FALL_TERM VARCHAR2(20 BYTE),
HR_GENDER_DESCRIPTION VARCHAR2(30 BYTE),
HR_CURRENT_ID VARCHAR2(10 BYTE),
IPEDS_RACE_ETHNICITY_DESCRIPTION VARCHAR2(50 BYTE),
OIR_FACULTY_STAFF_DETAILED_ROLE VARCHAR2(50 BYTE),
OIR_MGMT_FAC_AND_INSTR_IN_FALL_TERM VARCHAR2(50 BYTE),
OIR_OTHER_DEGREE_NOTES VARCHAR2(100 BYTE),
OIR_DOCTORATE VARCHAR2(20 BYTE),
OIR_HIGHEST_DEGREE_CATEGORY VARCHAR2(25 BYTE),
OIR_TERMINAL_DEGREE VARCHAR2(50 BYTE),
OIR_PRIMARY_ROLE VARCHAR2(25 BYTE),
HR_PIDM INTEGER,
OIR_POSITION VARCHAR2(25 BYTE),
OIR_FACULTY_SALARY NUMBER(13,3),
OIR_SCHOOL VARCHAR2(50 BYTE),
OIR_TENURE_STATUS VARCHAR2(50 BYTE),
PERSON_SKEY INTEGER,
CREATED_DATE DATE,
MODIFIED_DATE DATE,
OIR_IPEDS_OCCUPATION_CATEGORY VARCHAR2(1000 BYTE),
IPEDS_RANK_DESCRIPTION VARCHAR2(200 BYTE),
OIR_POSN_SEQUENCE INTEGER
);
CREATE TABLE VPAA_PPAGGENL_FIS --Degree Table
(
PIDM INTEGER NOT NULL,
VPAA_FACULTY_INSTITUTION_CODE VARCHAR2(6 CHAR) NOT NULL,
VPAA_FACULTY_INSTITUTION_NAME VARCHAR2(30 CHAR),
VPAA_FACULTY_OFFICIAL_TRANSCRIPT VARCHAR2(1 CHAR),
VPAA_FACULTY_OFFICIAL_TRANSCRIPT_RECEIPT_DATE DATE,
VPAA_FACULTY_DEGREE_CODE VARCHAR2(6 CHAR),
VPAA_FACULTY_DEGREE_DESCRIPTION VARCHAR2(30 CHAR),
VPAA_FACULTY_GRADUATION_DATE DATE,
VPAA_FACULTY_TERMINAL_DEGREE VARCHAR2(1 CHAR),
VPAA_FACULTY_SUBJECT_CODE VARCHAR2(4 CHAR),
VPAA_FACULTY_SUBJECT_DESCRIPTION VARCHAR2(30 CHAR),
VPAA_FACULTY_CIPC_CODE VARCHAR2(6 CHAR),
VPAA_FACULTY_ACAT_CODE VARCHAR2(2 CHAR),
CREATED_DATE DATE,
MODIFIED_DATE DATE,
PERSON_SKEY INTEGER,
VPAA_FACULTY_YEARS_SINCE_GRADUATION NUMBER,
VPAA_CURRENT_DEGREE INTEGER
);
--Crosswalk tables
CREATE TABLE WHOIRMGR.SUBJ_CODE_CROSSWALK
(
VPAA_FACULTY_DEGREE_CODE VARCHAR2(20 BYTE),
VPAA_FACULTY_DEGREE_DESCRIPTION VARCHAR2(50 BYTE),
VPAA_FACULTY_SUBJECT_CODE VARCHAR2(20 BYTE),
SUBJECT_CODE_DESCRIPTION VARCHAR2(50 BYTE),
PERFORMANCE_OR_THEORY VARCHAR2(20 BYTE),
OIR_TERMINAL_DEGREE_STATUS VARCHAR2(30 BYTE),
CREATED_DATE DATE,
MODIFIED_DATE DATE
);
CREATE TABLE WHOIRMGR.DIM_DEGREE_CROSSWALK
(
OIR_DEGREE_LEVEL VARCHAR2(50 BYTE),
VPAA_DEGREE_CODE VARCHAR2(25 BYTE),
VPAA_DEGREE_DESCRIPTION VARCHAR2(100 BYTE),
CREATED_DATE DATE,
MODIFIED_DATE DATE,
DEGREE_LEVEL_SKEY INTEGER,
OIR_DEGREE_HIERARCHY INTEGER,
OIR_TERMINAL_DEGREE_STATUS VARCHAR2(50 BYTE),
VPAA_FACULTY_SUBJECT_CODE VARCHAR2(25 BYTE),
VPAA_FACULTY_SUBJECT_CODE_DESCRIPTION VARCHAR2(100 BYTE),
PERFORMANCE_OR_THEORY VARCHAR2(25 BYTE)
);
CREATE TABLE WHOIRMGR.CLIC_INSTRUCTORS_CROSSWALK
(
HR_DEPARTMENT_HOME_ORGN_CODE NVARCHAR2(20),
HR_DEPARTMENT_HOME_ORGN_CODE_DESCRIPTION NVARCHAR2(50),
VPAA_FACULTY_DEGREE_CODE VARCHAR2(20 BYTE),
VPAA_FACULTY_DEGREE_DESCRIPTION VARCHAR2(50 BYTE),
VPAA_FACULTY_SUBJECT_CODE VARCHAR2(20 BYTE),
SUBJECT_CODE_DESCRIPTION VARCHAR2(50 BYTE),
OIR_TERMINAL_DEGREE_STATUS VARCHAR2(40 BYTE)
);
Update: This is what ended up working!! In the subquery, I had to do a left join instead of where id matches in both tables.
UPDATE SNP_FACULTY_CENSUS SFC
SET OIR_TERMINAL_DEGREE = (
WITH HIGHES_DEG_VALUE AS (
SELECT max(D.OIR_DEGREE_HIERARCHY) AS MAX_DEG
FROM VPAA_PPAGGENL_FIS V
JOIN DIM_DEGREE_CROSSWALK D
ON V.VPAA_FACULTY_DEGREE_CODE =
D.VPAA_DEGREE_CODE
left JOIN SNP_FACULTY_CENSUS S
ON V.PERSON_SKEY = S.PERSON_sKEY) --CHANGE TO = SRC.PERSON_SKEY
SELECT
CASE
WHEN MAX_DEG = 30
THEN --CHECK IF EXIST ON CROSSWALKS AND USE THAT VALUE IF IT DOES
(CASE
WHEN EXISTS (SELECT V.VPAA_FACULTY_DEGREE_CODE, --IF ON SUBJ CODE CROSSWALK
V.VPAA_FACULTY_SUBJECT_CODE
FROM VPAA_PPAGGENL_FIS V
JOIN DIM_DEGREE_CROSSWALK D
ON V.VPAA_FACULTY_DEGREE_CODE = D.VPAA_DEGREE_CODE
INNER JOIN SUBJ_CODE_CROSSWALK S
ON V.VPAA_FACULTY_DEGREE_CODE = S.VPAA_FACULTY_DEGREE_CODE
and S.VPAA_FACULTY_SUBJECT_CODE = V.VPAA_FACULTY_SUBJECT_CODE
left JOIN SNP_FACULTY_CENSUS S
ON V.PERSON_SKEY = S.PERSON_sKEY
--WHERE V.PERSON_SKEY = SFC.PERSON_SKEY ----CHANGE TO = SRC.PERSON_SKEY
)
THEN (SELECT DISTINCT -- USE TERMINAL VALUE FROM SUBJ CODE
CASE
WHEN s.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN s.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM SUBJ_CODE_CROSSWALK S
INNER JOIN VPAA_PPAGGENL_FIS V
ON V.VPAA_FACULTY_DEGREE_CODE =
S.VPAA_FACULTY_DEGREE_CODE
AND S.VPAA_FACULTY_SUBJECT_CODE =
V.VPAA_FACULTY_SUBJECT_CODE
left JOIN SNP_FACULTY_CENSUS S
ON V.PERSON_SKEY = S.PERSON_sKEY)
--WHERE V.PERSON_SKEY = SFC.PERSON_SKEY )----CHANGE TO = SRC.PERSON_SKEY )
WHEN EXISTS (SELECT DISTINCT V.VPAA_FACULTY_DEGREE_CODE, --IF ON CLIC INSTRUCTORS CROSSWALK
V.VPAA_FACULTY_SUBJECT_CODE,
I.OIR_EMPLOYEE_DEPARTMENT_HOME_ORG
FROM VPAA_PPAGGENL_FIS V
INNER JOIN CLIC_INSTRUCTORS_CROSSWALK C
ON V.VPAA_FACULTY_DEGREE_CODE = C.VPAA_FACULTY_DEGREE_CODE
AND V.VPAA_FACULTY_SUBJECT_CODE = C.VPAA_FACULTY_SUBJECT_CODE
INNER JOIN IPEDS_REPORT_DETAILS I ON V.PIDM = I.HR_PIDM
--WHERE V.PERSON_SKEY = SFC.PERSON_SKEY ----CHANGE TO = SRC.PERSON_SKEY
left JOIN SNP_FACULTY_CENSUS S
ON V.PERSON_SKEY = S.PERSON_sKEY
AND (I.OIR_EMPLOYEE_DEPARTMENT_HOME_ORG = '396000'
OR I.OIR_EMPLOYEE_DEPARTMENT_HOME_ORG = '396010')
)
THEN (SELECT DISTINCT
CASE
WHEN C.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN C.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM CLIC_INSTRUCTORS_CROSSWALK C
INNER JOIN VPAA_PPAGGENL_FIS V
ON C.VPAA_FACULTY_DEGREE_CODE =
V.VPAA_FACULTY_DEGREE_CODE
AND C.VPAA_FACULTY_SUBJECT_CODE =
V.VPAA_FACULTY_SUBJECT_CODE
--WHERE V.PERSON_SKEY = SFC.PERSON_SKEY)----CHANGE TO = SRC.PERSON_SKEY)
left JOIN SNP_FACULTY_CENSUS S
ON V.PERSON_SKEY = S.PERSON_sKEY)
ELSE --USE DIM DEGREE CROSSWALK NORMALLY
(SELECT DISTINCT
CASE
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM DIM_DEGREE_CROSSWALK D
JOIN VPAA_PPAGGENL_FIS V
ON V.VPAA_FACULTY_DEGREE_CODE =
D.VPAA_DEGREE_CODE
--WHERE V.PERSON_SKEY = SFC.PERSON_SKEY ----CHANGE TO = SRC.PERSON_SKEY)
left JOIN SNP_FACULTY_CENSUS S
ON V.PERSON_SKEY = S.PERSON_sKEY
ORDER BY OIR_DEGREE_HIERARCHY DESC
FETCH FIRST ROW ONLY )
END) --USE DIM DEGREE CROSSWALK FOR THE TERMINAL VALUE FOR THE MAX DEG THERE
WHEN MAX_DEG != 30
THEN
(SELECT DISTINCT
CASE
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM DIM_DEGREE_CROSSWALK D
JOIN VPAA_PPAGGENL_FIS V
ON V.VPAA_FACULTY_DEGREE_CODE =
D.VPAA_DEGREE_CODE
left JOIN SNP_FACULTY_CENSUS S
ON V.PERSON_SKEY = S.PERSON_sKEY
--WHERE V.PERSON_SKEY = SFC.PERSON_SKEY ----CHANGE TO = SRC.PERSON_SKEY)
ORDER BY OIR_DEGREE_HIERARCHY DESC
FETCH FIRST ROW ONLY )
ELSE NULL
END
FROM HIGHES_DEG_VALUE )
WHERE SFC.OIR_FALL_TERM = 'Fall 2019'
回答1:
When you use WITH with UPDATE you need to use alias for the update table. If not, it does not work.
Also, you can't invoke the UPDATED TABLE in the WITH CLAUSE, at that moment that table is unknown, that is why you get INVALID IDENTIFIER. To solve the problem you need to remove that condition and added at the end.
Try this one, but I am writing without even knowing the data model, so be aware.
UPDATE SNP_FACULTY_CENSUS SFC
SET OIR_TERMINAL_DEGREE = (
WITH HIGHES_DEG_VALUE AS (
SELECT max(D.OIR_DEGREE_HIERARCHY) AS MAX_DEG
FROM VPAA_PPAGGENL_FIS V
JOIN DIM_DEGREE_CROSSWALK D
ON V.VPAA_FACULTY_DEGREE_CODE = D.VPAA_DEGREE_CODE
LEFT JOIN SNP_FACULTY_CENSUS S
ON V.PERSON_SKEY = S.PERSON_SKEY
)
SELECT
CASE
WHEN MAX_DEG = 30
THEN --CHECK IF EXIST ON CROSSWALKS AND USE THAT VALUE IF IT DOES
(CASE
WHEN EXISTS (SELECT V.VPAA_FACULTY_DEGREE_CODE, --IF ON SUBJ CODE CROSSWALK
V.VPAA_FACULTY_SUBJECT_CODE
FROM VPAA_PPAGGENL_FIS V
JOIN DIM_DEGREE_CROSSWALK D
ON V.VPAA_FACULTY_DEGREE_CODE = D.VPAA_DEGREE_CODE
INNER JOIN SUBJ_CODE_CROSSWALK S
ON V.VPAA_FACULTY_DEGREE_CODE = S.VPAA_FACULTY_DEGREE_CODE
and S.VPAA_FACULTY_SUBJECT_CODE = V.VPAA_FACULTY_SUBJECT_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY ----CHANGE TO = SRC.PERSON_SKEY
)
THEN (SELECT DISTINCT -- USE TERMINAL VALUE FROM SUBJ CODE
CASE
WHEN s.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN s.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM SUBJ_CODE_CROSSWALK S
INNER JOIN VPAA_PPAGGENL_FIS V
ON V.VPAA_FACULTY_DEGREE_CODE =
S.VPAA_FACULTY_DEGREE_CODE
AND S.VPAA_FACULTY_SUBJECT_CODE =
V.VPAA_FACULTY_SUBJECT_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY )----CHANGE TO = SRC.PERSON_SKEY )
WHEN EXISTS (SELECT DISTINCT V.VPAA_FACULTY_DEGREE_CODE, --IF ON CLIC INSTRUCTORS CROSSWALK
V.VPAA_FACULTY_SUBJECT_CODE,
I.OIR_EMPLOYEE_DEPARTMENT_HOME_ORG
FROM VPAA_PPAGGENL_FIS V
INNER JOIN CLIC_INSTRUCTORS_CROSSWALK C
ON V.VPAA_FACULTY_DEGREE_CODE = C.VPAA_FACULTY_DEGREE_CODE
AND V.VPAA_FACULTY_SUBJECT_CODE = C.VPAA_FACULTY_SUBJECT_CODE
INNER JOIN IPEDS_REPORT_DETAILS I ON V.PIDM = I.HR_PIDM
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY ----CHANGE TO = SRC.PERSON_SKEY
AND (I.OIR_EMPLOYEE_DEPARTMENT_HOME_ORG = '396000'
OR I.OIR_EMPLOYEE_DEPARTMENT_HOME_ORG = '396010')
)
THEN (SELECT DISTINCT
CASE
WHEN C.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN C.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM CLIC_INSTRUCTORS_CROSSWALK C
INNER JOIN VPAA_PPAGGENL_FIS V
ON C.VPAA_FACULTY_DEGREE_CODE =
V.VPAA_FACULTY_DEGREE_CODE
AND C.VPAA_FACULTY_SUBJECT_CODE =
V.VPAA_FACULTY_SUBJECT_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY )----CHANGE TO = SRC.PERSON_SKEY)
ELSE --USE DIM DEGREE CROSSWALK NORMALLY
(SELECT DISTINCT
CASE
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM DIM_DEGREE_CROSSWALK D
JOIN VPAA_PPAGGENL_FIS V
ON V.VPAA_FACULTY_DEGREE_CODE =
D.VPAA_DEGREE_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY ----CHANGE TO = SRC.PERSON_SKEY)
ORDER BY OIR_DEGREE_HIERARCHY DESC
FETCH FIRST ROW ONLY )
END) --USE DIM DEGREE CROSSWALK FOR THE TERMINAL VALUE FOR THE MAX DEG THERE
WHEN MAX_DEG != 30
THEN
(SELECT DISTINCT
CASE
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Terminal'
THEN
'Yes'
WHEN D.OIR_TERMINAL_DEGREE_STATUS =
'Not terminal'
THEN
'No'
ELSE
'Unknown'
END
FROM DIM_DEGREE_CROSSWALK D
JOIN VPAA_PPAGGENL_FIS V
ON V.VPAA_FACULTY_DEGREE_CODE =
D.VPAA_DEGREE_CODE
WHERE V.PERSON_SKEY = SNP_FACULTY_CENSUS.PERSON_SKEY ----CHANGE TO = SRC.PERSON_SKEY)
ORDER BY OIR_DEGREE_HIERARCHY DESC
FETCH FIRST ROW ONLY )
ELSE NULL
END
FROM HIGHES_DEG_VALUE
) WHERE SNP_FACULTY_CENSUS.OIR_FALL_TERM = 'Fall 2019' ;
回答2:
A MERGE
will work best in this situation.
The idea of a MERGE
is to take data from a source table/query (specified in the USING
part of the MERGE
statement) and merge it into a destination table (specified in the MERGE INTO
part of the MERGE
statement).
Which row from the source dataset targets which row in the destination table is specified by on the merge condition (the condition after ON
in the MERGE
statement).
Now that you have set up the source and destination datasets and specified how to do the merging, you have to specify what happens when the merging is done. Your row from the source dataset may find a target in the destination table or may not. If it does you specify what should happen in the WHEN MATCHED
clause. If it doesn't find a target you specify what shoudl happen in the WHEN NOT MATCHED
clause.
Keeping these in mind, your MERGE
becomes
MERGE INTO snp_faculty_census sfc -- DESTINATION table
USING (SELECT v.person_skey,
MAX (d.oir_degree_hierarchy) AS max_deg
FROM vpaa_ppaggenl_fis v
JOIN dim_degree_crosswalk d
ON v.vpaa_faculty_degree_code = d.vpaa_degree_code) md -- SOURCE DATASET
ON (sfc.person_skey = md.person_skey) -- MERGEing condition
WHEN MATCHED THEN
UPDATE SET sfc.oir_terminal_degree = md.max_deg; -- WHAT HAPPENS WHEN MEGRED
That's all, in my understanding, is what you need to do. You can add a group by in your source dataset to prevent the same row from being merged again and again but it should work without that nevertheless.
来源:https://stackoverflow.com/questions/63180175/update-table-set-column-equal-to-select-where-id-matches-outside-of-query-in-ora