D
D
Depleted2021-11-25 16:47:47
SQL Server
Depleted, 2021-11-25 16:47:47

Why does COUNT count incorrectly?

PeopleTable (около 500 000 строк)
id | name | gender -- "М" /  "Ж" / NULL
ListTable (около 10 000 000 строк)
id | userID | date

Count the number of rows in the ListTable where the userID belongs to men/women.
Tried:
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 = 'Ж'

But the sum is more than the number of rows in the ListTable by about 15%.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-11-25
@Depleted

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) 
;

MS SQL Fiddle

R
Rsa97, 2021-11-25
@Rsa97

LEFT JOIN. This means that in both cases you take into account rows from the ListTable for which there is no corresponding entry in the PeopleTable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question