1

我有一个查询,看起来像这样:

SELECT
cat_code, cat_desc,
count(mthtrn > 0),
count(ltmtrn)
from my_temp_table
group by cat_code
order by mthsls desc;

我怎样才能让我的第一个count()只获得大于零的值的计数,但在第二个中获得所有这些?我可以做得比 with 更大,sum()但我似乎无法做到 if for count,即使没有抛出错误。

理想情况下,这会很棒:

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;

我知道它是无效的,但类似的东西。

4

1 回答 1

0

利用:

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;
于 2013-05-22T17:43:55.253 回答