How to express count(distinct) with subquery in My

2019-09-08 01:02发布

A query results a certain number. The query is:

select
count(distinct case when (A or B or C) and D then table_a.field1 else null end)
from table_a
    left join table_b on table_b.x = table_a.y
group by table_a.y
;

where A, B, C and D are given conditions. Now, written in this form:

select
sum((select count(1) from table_b where table_b.x = table_a.y and ((A or B or C) and D) ))
from table_a
    left join table_b on table_b.x = table_a.y
group by table_a.y
;

the result does not match the one we got with count(distinct).

What is the correct way of writing count(distinct) with a subquery?

1条回答
Animai°情兽
2楼-- · 2019-09-08 01:15

It's not at all clear why you need a subquery. You still have the JOIN, so that subquery is potentially going to be "counting" the same rows multiple times.

If you want to get the number of distinct values for field1 in table_a which meets a set of criteria (on table_a), then you don't really need a subquery on table_b to get that. At least, I don't see anyway that you can get that result using a subquery on table_b.

Here's an example that returns an equivalent result:

 select (select sum(1) as mycount
           from ( select a.field1 
                    from table_a a
                    left join table_b on table_b.x = a.y
                   where a.y = t.y
                     and ( (A or B or C) and D )
                     and a.field1 IS NOT NULL
                   group by a.field1
                ) s
        ) as mycount
   from table_a t
  group by t.y

That's really the only way I know to get something equivalent to a COUNT(DISTINCT expr). You've got to do a SELECT expr FROM ... WHERE expr IS NOT NULL GROUP BY expr, and then count the rows it returns. In this case, you could use either a COUNT(1) or a SUM(1).

(I'm not at all sure that answers the question you were asking, but it's my best shot at it.)

(We note that in your original query, you have a GROUP BY table_a.y, so that query can return multiple rows, each with its own count.

查看更多
登录 后发表回答