你可以把你所有的
UNION
转换为如下所示的单个查询。
select
issuer,acquirer,destination,
case
when issuer= acquirer and issuer <> destination then issuer + " is issuer-acquirer"
when issuer = destination and acquirer <> destination then issuer +" as issuer-destination"
when issuer<> acquirer and acquirer= destination then acquirer + " as acquirer-destination"
when issuer<> acquirer and issuer <> destination then issuer +" as issuer only"
when issuer<>acquirer and destination <> acquirer then acquirer + " as acquirer only"
when issuer<>destination and acquirer<>destination then destination + " as destination only"
else "unknown"
end as bank_role
from trx_data
:为了处理不同的场景,我创建了一个示例,它位于SQL Server中,但它应该可以在所有数据库中工作。
select *,
case
when issuer=t.Identifier and acquirer=t.Identifier and destination<>t.Identifier then t.Identifier +' as issuer-acquirer'
when issuer=t.Identifier and acquirer<>t.Identifier and destination=t.Identifier then t.Identifier +' as issuer-destination'
when issuer<>t.Identifier and acquirer=t.Identifier and destination=t.Identifier then t.Identifier + ' as acquirer-destination'
when issuer=t.Identifier and acquirer<>t.Identifier and destination<>t.Identifier then t.Identifier +' as issuer only'
when issuer<>t.Identifier and acquirer=t.Identifier and destination<>t.Identifier then t.Identifier + ' as acquirer only'
when issuer<>t.Identifier and acquirer<>t.Identifier and destination=t.Identifier then t.Identifier + ' as destination only'
else 'unknown'
end as bank_role
from @trx_data d
cross join
(
select distinct issuer as 'Identifier' from @trx_data
union
select distinct acquirer as 'Identifier' from @trx_data
union
select distinct destination as 'Identifier' from @trx_data
)t
order by t.Identifier
Online Demo