希望为初始查询中缺少的角色生成新行

Looking to generate new rows for roles missing in initial query

我有一组当前数据,如下所示:

Table 姓名:Project_EffectiveDating

ProjectCode RoleCode EffectiveDate
1a1 A 1/1/2020
1a1 B 1/1/2020
1a1 C 1/1/2020
1a1 D 1/1/2020
1a1 E 1/1/2020
1a1 A 2/1/2020
ProjectCode RoleCode EffectiveDate
1a1 A 1/1/2020
1a1 B 1/1/2020
1a1 C 1/1/2020
1a1 D 1/1/2020
1a1 E 1/1/2020
1a1 A 2/1/2020
1a1 B 2/1/2020
1a1 C 2/1/2020
1a1 D 2/1/2020
1a1 E 2/1/2020

其中顶部 table 是现有数据,底部 table 是所需的输出。

我一直在尝试通过以下方式加入 table 本身:

select 
  dbd.projectcode
, dbd.role
, dbd.effectivedate
from Project_EffectiveDating as dbd 
left join Project_EffectiveDating as dad 
     on dad.projectcode = dbd.projectcode 
     and dbd.rolecode <> dad.rolecode 

但显然这是行不通的。我正在考虑使用 WHERE NOT IN() 做联合声明 但我不知所措。任何帮助将不胜感激。

您似乎想要 ProjectCode/RoleCodeEffectiveDate 对之间的笛卡尔积:

select *
from (select distinct ProjectCode, RoleCode
      from Project_EffectiveDating
     ) pr cross join
     (select distinct EffectiveDate
      from Project_EffectiveDating
     ) e;

如果要添加不存在的行:

insert into Project_EffectiveDating (ProjectCode, RoleCode, EffectiveDate)
    select pr.ProjectCode, pr.RoleCode, e.EffectiveDate
    from (select distinct ProjectCode, RoleCode
          from Project_EffectiveDating
         ) pr cross join
         (select distinct EffectiveDate
          from Project_EffectiveDating
         ) e left join
         Project_EffectiveDating ped
         on ped.ProjectCode = pr.ProjectCode and
            ped.RoleCode = pr.RoleCode and
            ped.EffectiveDate = pr.EffectiveDate
    where ped.ProjectCode is null;