How to express count(distinct) with subquery in MySQL?

左心房为你撑大大i 提交于 2019-12-25 15:50:54

问题


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:


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.



来源:https://stackoverflow.com/questions/11439440/how-to-express-countdistinct-with-subquery-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!