簡體   English   中英

sql-將3列中的匹配值分組

[英]sql - grouping matched value from 3 column

我有一個名為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
;

我知道這不好,對此有更好的方法嗎?

您可以將所有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

在線演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM