Help with a SQL MINUS statement
I have 3 tables, Reporter_Vacation, Dropper, Vacation_Temp.
Reporter_Vacation has Reporter, Start_dt, End_dt (the 3 combined are unique)
Dropper has Dropper_ID and Reporter (both are unique)
Vacation_Temp has Dropper_ID, Start_dt, End_dt (the 3 combined are unique)
How can I find what Reporter, Start_dt, and End_dt are in Vacation_Temp that are not in Reporter_Vacation?
SELECT DROPPER_ID, BEGIN_DT, END_DT
FROM VACATION_TEST
MINUS
SELECT DROPPER.DROPPER_ID, REPORTER_VACATION.BEGIN_DT, REPORTER_VACATION.END_DT
FROM REPORTER_VACATION, DROPPER
WHERE DROPPER.REPORTER = REPORT开发者_JAVA技巧ER_VACATION.REPORTER;
An alternative approach that is more standardised and more indexable (hence often faster) than MINUS is a null-join.
Use a LEFT JOIN to bring in the table you don't want, leaving NULL where there is no value in the table. Then test for that NULL in the WHERE clause. Expanding that to two joined tables:
SELECT vacation_test.dropper_id, vacation_test.begin_dt, vacation_test.end_dt
FROM vacation_test
LEFT JOIN dropper ON
dropper.dropper_id=vacation_test.dropper_id
LEFT JOIN reporter_vacation ON
reporter_vacation.dropper_id=dropper.dropper_id AND
reporter_vacation.begin_dt=vacation_test.begin_dt AND
reporter_vacation.end_dt=vacation_test.end_dt
WHERE reporter_vacation.begin_dt IS NULL
精彩评论