sql - группировка совпадающих значений из 3 столбцов - PullRequest
0 голосов
/ 21 февраля 2019

у меня есть таблица с именем trx_data, допустим, она содержит:

issuer  acquirer    destination
A       A           C
A       B           A
B       A           A
B       A           C
C       B           A
A       B           C

я хочу сгруппировать A, B, C в:

  1. значение в качестве эмитентатолько
  2. значение только для получателя
  3. значение только для получателя
  4. значение как эмитента и получателя
  5. значение как эмитента и получателя
  6. значениекак приобретатель и пункт назначения

это мой код

select bank_role, count(*) from(
select
issuer,acquirer,destination,
case
when issuer="A" and acquirer="A" and destination<>"A" then "A as issuer-acquirer"
when issuer="A" and acquirer<>"A" and destination="A" then "A as issuer-destination"
when issuer<>"A" and acquirer="A" and destination="A" then "A as acquirer-destination"
when issuer="A" and acquirer<>"A" and destination<>"A" then "A as issuer only"
when issuer<>"A" and acquirer="A" and destination<>"A" then "A as acquirer only"
when issuer<>"A" and acquirer<>"A" and destination="A" then "A as destination only"
else "unknown"
end as bank_role
from trx_data
union all
select
issuer,acquirer,destination,
case
when issuer="B" and acquirer="B" and destination<>"B" then "B as issuer-acquirer"
when issuer="B" and acquirer<>"B" and destination="B" then "B as issuer-destination"
when issuer<>"B" and acquirer="B" and destination="B" then "B as acquirer-destination"
when issuer="B" and acquirer<>"B" and destination<>"B" then "B as issuer only"
when issuer<>"B" and acquirer="B" and destination<>"B" then "B as acquirer only"
when issuer<>"B" and acquirer<>"B" and destination="B" then "B as destination only"
else "unknown"
end as bank_role
from trx_data
union all
select
issuer,acquirer,destination,
case
when issuer="C" and acquirer="C" and destination<>"C" then "C as issuer-acquirer"
when issuer="C" and acquirer<>"C" and destination="C" then "C as issuer-destination"
when issuer<>"C" and acquirer="C" and destination="C" then "C as acquirer-destination"
when issuer="C" and acquirer<>"C" and destination<>"C" then "C as issuer only"
when issuer<>"C" and acquirer="C" and destination<>"C" then "C as acquirer only"
when issuer<>"C" and acquirer<>"C" and destination="C" then "C as destination only"
else "unknown"
end as bank_role
from trx_data)zxc
group by bank_role
;

я знаю, что это нехорошо, есть ли лучший подход для этого?

1 Ответ

0 голосов
/ 21 февраля 2019

Вы можете объединить все свои 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

Онлайн-демонстрация

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...