简体   繁体   中英

distinct & count grouping distinct items?

I'm trying to create one query to select some data. I need to select them only as DISTINCT but count each group of items inside each container eg

results: CATEGORY EMAIL

  1. Jo1 user1@www.com
  2. Jo1 user2@www.com
  3. Jo1 user3@www.com
  4. Jo2 user4@www.com
  5. Jo2 user5@www.com

what I need: Jo1 3 -> count unique users Jo2 2 -> count unique users

I can do it with distinct to split them but can't find a way of counting separate groups. SELECT count(DISTINCT CATEGORY) ... counting all users.

Thnanks,

You should be COUNT(DISTINCT) ing email not category :

SELECT `CATEGORY`, COUNT(DISTINCT `EMAIL`)
FROM the_table
GROUP BY `CATEGORY`
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM