Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用大于号进行计数_Mysql_Count_Aggregate Functions - Fatal编程技术网

Mysql 使用大于号进行计数

Mysql 使用大于号进行计数,mysql,count,aggregate-functions,Mysql,Count,Aggregate Functions,我有一个查询,如下所示: SELECT cat_code, cat_desc, count(mthtrn > 0), count(ltmtrn) from my_temp_table group by cat_code order by mthsls desc; 如何让我的第一个count()只获取值大于零的计数,而在第二个中获取所有值?我可以用sum()做一个大于,但对于计数,我似乎做不到if,即使没有抛出错误 理想情况下,这将非常棒: SELECT cat_code, cat_des

我有一个查询,如下所示:

SELECT
cat_code, cat_desc,
count(mthtrn > 0),
count(ltmtrn)
from my_temp_table
group by cat_code
order by mthsls desc;
如何让我的第一个
count()
只获取值大于零的计数,而在第二个中获取所有值?我可以用
sum()
做一个大于,但对于计数,我似乎做不到if,即使没有抛出错误

理想情况下,这将非常棒:

SELECT
cat_code, cat_desc,
count(distinct member_id having mthtrn > 0),
count(ltmtrn)
from my_temp_table
group by cat_code
order by mthsls desc;
我知道它是无效的,但类似的东西。

使用:

SELECT
cat_code, cat_desc,
count(distinct case when mthtrn > 0 then member_id end),
count(ltmtrn)
from my_temp_table
group by cat_code
order by mthsls desc;