0

I have the following table for which I would like to obtain result based on the number of different TYPEs available for every CONTRACT_ID.

Table:

______________________________________________________________________________________
|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            |
______________________________________________________________________________________

Result:

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

i.e, The ROLES column in the result table should be the number of different TYPEs for a distinct CONTRACT_ID.

Can anyone help?

1
  • I have surfed but still couldnt get any idea on how to count the number of different varchar values @KaushikNayak! Commented Sep 25, 2017 at 12:54

2 Answers 2

1
SELECT max(source) , contract_id ,
count( DISTINCT type) roles , max(address), max(phone) 
FROM Table group by contract_id ;
Sign up to request clarification or add additional context in comments.

Comments

0

I think you are looking for count(distinct):

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

This is not "filtering". It is "aggregation".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.