计算大小写时 *

Count case when *

我在 count case 语句中遇到错误,我希望能得到一些帮助。

我的代码行是

count(case when product = 'classic' then * end)

我收到的错误消息是

ERROR: syntax error at or near "*" LINE 4: ...t(case when initial_product_line = 'classic' then * end) as ...

能不能不数*? ^

我一般用sum():

sum(case when product = 'classic' then 1 else 0 end)

A​​mazon Redshift 不支持最新的 Postgres 功能,但 Postgres 实现了过滤器子句,这很好:

count(*) filter (where product = 'classic')

我之所以提出这个问题是因为它是标准语法并且与您的查询版本相关(并且由相关数据库支持)。

*SQL 中有特殊含义,您需要 1columnname 来代替:

count(case when product = 'classic' then col end)

您也可以将其简化为:

sum(case when product = 'classic' then 1 else 0 end)