需要在单独的行中将 NULL 显示为缺失属性的值

Need to show NULL as a value of missing attributes in a separate row

我在 MSSQL 17 中有一个 table,如下所示:

Attribute   Value   Code
Country     Canada  AA
Source      EFT     AA
Manager     Ahmad   AA
Source      EFT     BB
Manager     Mike    BB
Country     Brazil  CC
Source      Cash    CC

我需要所有代码具有相同的行数,显示相同的属性,如果 table 中不存在,则值应为 NULL。我正在寻找的输出:

Attribute   Value   Code
Country     Canada  AA
Source      EFT     AA
Manager     Ahmad   AA
Country     NULL    BB
Source      EFT     BB
Manager     Mike    BB
Country     Brazil  CC
Source      Cash    CC
Manager     NULL    CC

你能帮帮我吗?赞赏!

使用 cross join 生成行,然后使用 left join 引入现有值:

select a.attribute, t.value, c.code
from (select distinct code from t) c cross join
     (select distinct attribute from t) a left join
     t
     on c.code = t.code and a.attribute = t.attribute