我有两张桌子,如下:
t_Normal ---------------------------------------------- FieldKey | FieldLabel | FieldValue ---------------------------------------------- greet_hw | Hello, world! | HELLOWORLD ---------------------------------------------- greet_ws | What's shakin? | WHATISSHAKING ---------------------------------------------- greet_hh | How's it hangin?| HOWDOESITHANG ---------------------------------------------- t_Override ------------------------------------------------------------ FieldKey | FieldLabel | FieldValue | FieldStatus ------------------------------------------------------------ greet_ws | What's shakin? | WHATISSHAKING | Retired ------------------------------------------------------------ greet_s | Sup!?!?? | SUPELEVEN | Active ------------------------------------------------------------ greet_hh | How swings it? | HOWDOESITHANG | Active ------------------------------------------------------------
我可以加入他们以获得这个:
v_FieldMaster --------------------------------------------- FieldKey | FieldLabel | FieldValue --------------------------------------------- greet_hw | Hello, world! | HELLOWORLD --------------------------------------------- greet_s | Sup!?!?? | SUPELEVEN --------------------------------------------- greet_hh | How swings it? | HOWDOESITHANG ---------------------------------------------
所以它加入了这样生成v_FieldMaster:
Quickest way to combine two identical structured tables without duplicate entries让我参与其中,但我还没有想出如何将该技术与t_Override.FieldStatus上的where子句相结合。任何建议都非常欢迎!
答案 0 :(得分:1)
执行完整加入,但只选择没有覆盖或覆盖为“活动”的行。合并每个字段以优先处理覆盖值(如果它们在那里)。
SELECT
COALESCE(o.FieldKey ,n.FieldKey ) AS FieldKey ,
COALESCE(o.FieldLabel ,n.FieldLabel ) AS FieldLabel ,
COALESCE(o.FieldStatus,n.FieldStatus) AS FieldStatus
FROM t_Normal n
FULL JOIN t_Override o ON (o.FieldKey = n.FieldKey)
WHERE (o.FieldKey IS NULL OR o.FieldStatus = 'Active')