简体   繁体   中英

SQL: Condition in Left outer Join

Election:

EID PID    PlanCode     Coverage
2   49791   DELTA        FAMILY
2   49791   LIGNA        FAMILY
2   49791   BSP          FAMILY
2   49792   BSP          FAMILY
2   49792   LIGNA        FAMILY
2   49792   DELTA        FAMILY
2   49793   LIGNA        FAMILY

relationship Table:

EID     PID
2   49791
2   49792
2   49793

table Desc:

Relationship: Employee has 3 dependents

Election:For each Pid, an employee chooses a particular Plan

Problem:

As Pid:49793 enroll in LigNa plan only, trying to add 2 rows for remaining plan ie delta,BSP row

Can we do it as plan name is not fixed, but we know of dependents in relationship table?

Please suggest..

PS: Its working now using cross Join

If am not wrong you are trying to insert the missing PlanCode for each EID,PID combination. Am not very sure about the link of LEFT JOIN usage with this question unless I missed something

INSERT INTO Election
            (EID,PID,PlanCode,Coverage)
SELECT A.EID,A.PID,B.PlanCode,A.Coverage
FROM   (SELECT DISTINCT EID,PID,Coverage
        FROM   Election) A
       CROSS JOIN (SELECT DISTINCT PlanCode
                   FROM   Election) B
WHERE  NOT EXISTS (SELECT 1
                   FROM   Election C
                   WHERE  A.EID = C.EID
                          AND A.PID = C.PID
                          AND B.PlanCode = C.PlanCode) 

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