Count number of unique values

后端 未结 4 1116
予麋鹿
予麋鹿 2020-11-29 07:53

If I have three columns:

orderNumber, name, email

and I would like to count how many unique emails are in the table how would I go about do

相关标签:
4条回答
  • 2020-11-29 08:19

    use

    SELECT count( DISTINCT(email) ) FROM orders
    

    Distinct provide unique email ids and then simply count them.

    0 讨论(0)
  • 2020-11-29 08:29

    For best performance you should use:

    SELECT 
    sub.email, 
    count(1) as 'count_unique' 
    FROM 
    (SELECT email FROM orders GROUP by email) sub
    
    0 讨论(0)
  • 2020-11-29 08:33

    The accepted soultion doesn't work for me - it returns a "1" for each unique email address in the table.

    This is what I had to do to get the info I needed:

    select email, count(email) AS total from sysAccessLog group by email order by total desc
    

    Which returns a list of email addresses and the number of occurrences.

    0 讨论(0)
  • 2020-11-29 08:38
    SELECT  count(DISTINCT(email)) FROM orders
    

    its different from your posting, since its filters out the duplicates before counting it

    0 讨论(0)
提交回复
热议问题