Search code examples
mysqlsqlcountdistinct

How can I use select count (distinct x) in order to count two values in the same table and get the the two distinct values in my output?


I am trying to use count (distinct) in order to count the number of distinct values for two different values in the same table. The output I am trying to get should have both of the distinct values as two separate columns. I have been trying different methods but I keep on getting syntax errors. I am able to to count just one of the values without any problem, but I can not figure out the syntax for two.

For example, I can count one by executing:

select count(distinct origin) as distinctOrigin
from flights;

But I want to do something along the lines of this:

select count(distinct origin) as distinctOrigin and
select count(distinct dest) as distinctDestination
from flights;

And get two output values as two separate columns.


Solution

  • Can't you just have two expressions in the select?

    select count(distinct origin) as distinctOrigin,
           count(distinct dest) as distinctDest
    from flights ;