2017-09-25 149 views
0

我有以下表格,我希望根據不同類型的編號獲得結果,每CONTRACT_ID可用。Oracle - 根據列值進行篩選

表:

______________________________________________________________________________________ 
|Source    | CONTRACT_ID | TYPE  | ADDRESS | PHONE   | 
______________________________________________________________________________________ 
|Source1   | 1234  | INSURED  | unique1 | 4321   | 
______________________________________________________________________________________ 
|Source1   | 1234  | INSURED  | unique1 | 4321   | 
______________________________________________________________________________________ 
|Source1   | 1234  | DRIVER  | unique1 | 4321   | 
______________________________________________________________________________________ 
|Source1   | 1234  | INSURED  | unique1 | 4321   | 
______________________________________________________________________________________ 
|Source2   | 2345  | INSURED  | unique2 | 5678   | 
______________________________________________________________________________________ 
|Source2   | 2345  | DRIVER  | unique2 | 5678   | 
______________________________________________________________________________________ 
|Source2   | 2345  | CONTRACT | unique2 | 5678   | 
______________________________________________________________________________________ 

結果:

______________________________________________________________________________________ 
|Source    | CONTRACT_ID | ROLES  | ADDRESS | PHONE   | 
______________________________________________________________________________________ 
|Source1   | 1234  | 2   | unique1 | 4321   | 
______________________________________________________________________________________ 
|Source2   | 2345  | 3   | unique2 | 5678   | 
______________________________________________________________________________________ 

即在結果表中的ROLES列應爲不同TYPE S的一個獨特的數CONTRACT_ID

任何人都可以幫忙嗎?

+0

你到目前爲止嘗試過什麼? –

+0

我已經衝浪,但仍然無法得到如何計算不同的varchar值@KaushikNayak的數量! – Nidheesh

回答

0

我認爲你正在尋找count(distinct)

select Source, CONTRACT_ID, ADDRESS, PHONE, count(distinct type) as roles 
from t 
group by Source, CONTRACT_ID, ADDRESS, PHONE ; 

這不是 「過濾」。這是「聚合」。

1
SELECT max(source) , contract_id , 
count(DISTINCT type) roles , max(address), max(phone) 
FROM Table group by contract_id ;