简体   繁体   中英

KSQL : Left Join with Not Equal where condition is not satisfying the result

Please find the below issue and confirm for the same.

Step-01 : Based on the joining condition , getting the value from both table and populating. As no matching value in table B, all columns are being populated with NULL Value.

Column: B.OP_TYPE,B.DEMO_ID

ksql> SELECT PLAN_ID,A_OP_TYPE,B.OP_TYPE,A.PRIMARY_DEMO_ID,B.DEMO_ID \
FROM TBL_PLN_PRO_DIV_SDIV A \
LEFT JOIN  TBL_MS_TARGET_GROUP11 B \
ON (A.PRIMARY_DEMO_ID=B.DEMO_ID);
382591 | U | null | 3085 | null

Same select query with where condition is giving the same result as expected .

ksql> SELECT PLAN_ID,A_OP_TYPE,B.OP_TYPE,A.PRIMARY_DEMO_ID,B.DEMO_ID \
FROM TBL_PLN_PRO_DIV_SDIV A \
LEFT JOIN  TBL_MS_TARGET_GROUP11 B \
ON (A.PRIMARY_DEMO_ID=B.DEMO_ID) WHERE B.OP_TYPE IS NULL;
382591 | U | null | 3085 | null

But when we tried to select with Not Equal where condition , respective query is not giving the proper result. B.OP_TYPE != 'D' - This is where condition which include B.OP_TYPE is null

ksql> SELECT PLAN_ID,A_OP_TYPE,B.OP_TYPE,A.PRIMARY_DEMO_ID,B.DEMO_ID \
FROM TBL_PLN_PRO_DIV_SDIV A \
LEFT JOIN  TBL_MS_TARGET_GROUP11 B \
ON (A.PRIMARY_DEMO_ID=B.DEMO_ID) WHERE B.OP_TYPE != 'D';

What you've described so far is not inconsistent with how SQL behaves. The important thing is that NULL means no value. So FOO != 'B' does not match where FOO is NULL .

You've not provided sample data for TBL_PLN_PRO_DIV_SDIV and TBL_MS_TARGET_GROUP11 so it's hard to help definitively here.

If your query didn't return rows where A_OP_TYPE != 'D' then I'd agree something wasn't right, because you've shown that there are rows where A_OP_TYPE='U' (ie != 'D' ). But, if you have no matching records from TBL_MS_TARGET_GROUP11 in which the OP_TYPE has a value other than D , you won't get a match.

Employee Table:

select empno,ename,job,deptno from emp1;

7839 KING PRESIDENT 10 7698 BLAKE MANAGER 30 7902 FORD ANALYST 20 7369 SMITH CLERK 20

select * from dept1;

10 ACCOUNTING NEW YORK 30 SALES CHICAGO

Left join without any where condition :

select empno,ename,job,a.deptno,a.DEPTNO,DNAME from emp1 a left join dept1 b on (a.DEPTNO = b.DEPTNO)

7839    KING    PRESIDENT   10  10  ACCOUNTING
7698    BLAKE   MANAGER     30  30  SALES
7369    SMITH   CLERK       20  20  
7902    FORD    ANALYST     20  20  

where condition with Null value 

select empno,ename,job,a.deptno,a.DEPTNO,DNAME from emp1 a left join dept1 b on (a.DEPTNO = b.DEPTNO) where DNAME is null;

7369    SMITH   CLERK   20  20  
7902    FORD    ANALYST 20  20  

select empno,ename,job,a.deptno,a.DEPTNO,DNAME from emp1 a left join dept1 b on (a.DEPTNO = b.DEPTNO) where DNAME != 'ACCOUNTING';

7698 BLAKE MANAGER 30 30 SALES

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM