Answer the question
In order to leave comments, you need to log in
Why does COUNT count incorrectly?
PeopleTable (около 500 000 строк)
id | name | gender -- "М" / "Ж" / NULL
ListTable (около 10 000 000 строк)
id | userID | date
SELECT COUNT(list.id) AS men
FROM ListTable AS list
LEFT JOIN PeopleTable AS people ON (list.userID = people.id)
WHERE people.gender = 'М'".
...
WHERE people.gender = 'Ж'
Answer the question
In order to leave comments, you need to log in
Try:
SELECT
COUNT(DISTINCT CASE WHEN people.gender = 'M' THEN list.id END) AS mens,
COUNT(DISTINCT CASE WHEN people.gender = 'F' THEN list.id END) AS womens,
COUNT(DISTINCT CASE WHEN people.gender IS NULL THEN list.id END) AS unknown
FROM ListTable AS list
LEFT JOIN PeopleTable AS people ON (list.userID = people.id)
;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question