0

I have 2 tables. Table table1 like this

specialty     doctor
  1              A
  1              B
  2              A
  2              C

Table table2 like this:

specialty     doctor
  1              A
  1              D
  2              C

I want to count distinct doctor from both the table group by specialty and have the output like this

specialty      doctor
  1              3
  2              2

(specialty 1 has 3 doctors:A, B, D; specialty 2 has 2 doctors: A, C)

Really appreciate your help

1 Answer 1

1

You can use union to bring the tables together and then group by:

select specialty, count(*)
from ((select specialty, doctor from table1) union
      (select specialty, doctor from table2)
     ) t
group by specialty;

Note the use of union here and not union all. You specifically want to eliminate duplicates.

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.