sql - Show column value of one of left joins -
in query shows 1 row if sal.id_amenaza
exists in mft
or mfa
schemas, want show mft.id_amenaza
or mfa.id_amenaza
(if exist in table) in resulting row.
in actual query dont require id_amenaza. how can show?
select sal.id_salvaguarda, sal.descripcion, sal.eficacia agr_salvaguardas sal left join agr_mit_frec_tipo mft on sal.id_salvaguarda = mft.id_salvaguarda , mft.id_amenaza = 5043 left join agr_mit_frec_act mfa on sal.id_salvaguarda = mfa.id_salvaguarda , mfa.id_amenaza = 5043 mft.id_salvaguarda not null or mfa.id_salvaguarda not null group sal.id_salvaguarda, sal.descripcion, sal.eficacia
i need obtain table this:
id_salvaguarda | descripcion | eficacia | id_amenaza 5061 | pre-01 | 100 | 5043
thank in advance.
this should work you:
select sal.id_salvaguarda, sal.descripcion, sal.eficacia, 5043 id_amenaza agr_salvaguardas sal left join agr_mit_frec_tipo mft on sal.id_salvaguarda = mft.id_salvaguarda , mft.id_amenaza = 5043 left join agr_mit_frec_act mfa on sal.id_salvaguarda = mfa.id_salvaguarda , mfa.id_amenaza = 5043 mft.id_salvaguarda not null or mfa.id_salvaguarda not null group sal.id_salvaguarda, sal.descripcion, sal.eficacia;
because query guarantees there @ least 1 match.
however, more general solution is:
select sal.id_salvaguarda, sal.descripcion, sal.eficacia, coalesce(mft.id_amenaza, mfa.id_amenaza) id_amenaza . . .
the function coalesce()
returns first value not null
in list of arguments.
Comments
Post a Comment