guigoz
guigoz

Reputation: 704

mysql GROUP BY doesn't match DISTINCT

I must be missing an obvious thing since after hours of testing I seem to be blind

table 1 created with

CREATE table1
SELECT column1,column2,SUM(column3) 
FROM table2 
WHERE condition1 
GROUP BY column1,column2

doesn't have the expected row count

So I wanted to check table2 with

SELECT COUNT(DISTINCT column1,column2) FROM table2 WHERE condition1

and that gave me the expected row count

What is wrong in my way of proceeding ?

Upvotes: 0

Views: 145

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269753

The numbers would be different if either column1 or column2 were NULL.

count(distinct) does not count null values. When used with multiple arguments, this is true even when only one expression is null.

However, null values go on separate rows in a group by.

Upvotes: 1

Related Questions