I need a get some rows of two tables with join that shoud have one value in a column (1407) but shouldn’t have other value (1403)
These is the tables and the query:
select a.job, a.date, b.group from log a inner join active_tmp b on a.jobno=b.jobno and a.no=b.no where b.list = 'N' AND LOGDATE = TO_CHAR(TRUNC(SYSDATE),'YYYYMMDD') and a.job not like 'HOUSE%' and a.job not like 'CAR%' and (errorCode=1047 and errorCode<>1403); LOG JOB DATE LOGDATE JOBNO NO errorCode MAM 20220123 20220125 33 22 1047 MAM 20220123 20220125 33 22 1403 DAD 20220122 20220125 11 99 1047 MAM 20220122 20220125 33 22 0323 DAD 20220122 20220125 11 99 0444 ACTIVE_TMP JOB JOBNO NO GROUP LIST MAM 33 22 LAPTOP N MAM 33 22 LAPTOP N DAD 11 99 KEY N But I get: MAM,20220123,LAPTOP DAD,20220122,KEY
I need:
DAD,20220122,KEY
Because MAM have both codes (1047 and 1043).
Advertisement
Answer
To rephrase, I think you mean “I want to return matching rows that have error code 1047 but for which the same values of jobno, no, list do not have a corresponding row with error code 1403”
This part is redundant:
AND (errorCode = 1047 AND errorCode <> 1403);
If you are saying errorCode must be 1047, you are also saying it is not equal to 1403.
I think you want to select some rows into some result set, then check that there’s not another row that disqualifies one of the selected rows from the final result.
So,
SELECT a.job, a.date, b.group FROM _log a INNER JOIN _active_tmp b ON a.jobno = b.jobno AND a.no = b.no WHERE b.list = 'N' AND LOGDATE = TO_CHAR(CURRENT_TIMESTAMP,'YYYYMMDD') AND a.job NOT LIKE 'HOUSE%' AND a.job NOT LIKE 'CAR%' AND a.errorCode = 1047 AND NOT EXISTS (SELECT 1 FROM _log c INNER JOIN _active_tmp d ON c.jobno = d.jobno AND c.no = d.no WHERE a.job = c.job AND a.date = c.date AND b.group = d.group AND c.errorCode = 1403)
We select the rows that satisfy the join and have error code 1047 then subtract from that set those rows that also satisfy the join but have error code 1403. You could possibly make this more terse using CTE or a temp table, but this works too.
Note I had to change a few things to make it work in my engine (Postgres), so you may have to change a few things back to Oracle.