1
ID ArrivalPort DeparturePort
1  A           B
1  A           C
2  A           B
2  C           D

How do I do a unique/distinct count of both the ArrivalPort and DeparturePort by ID?

Out
ID   Count
1    3
2    4

Thanks!

1 Answer 1

2

You can do a union in a derived tabled then count:

select ID, count(distinct Port) as Count from
(select id, ArrivalPort as Port from table_name
union all select id, DeparturePort from table_name) t
group by id;

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.