计算*时的大小写

时间:2018-11-16 16:56:41

标签: sql amazon-redshift psql

我的计数案例陈述中出现错误,希望能得到一些帮助。

我的代码行是

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

我收到的错误消息是

  

错误:“ *”或附近的语法错误   第4行:... t(当initial_product_line ='classic'然后* end的情况)为...

我可以不数*吗?                                                              ^

2 个答案:

答案 0 :(得分:2)

*SQL中具有特殊含义,而不是1columnname

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

您也可以将其简化为:

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

答案 1 :(得分:1)

我通常使用sum()

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

Amazon Redshift不支持最新的Postgres功能,但是Postgres实现了filter子句,这很不错:

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

我之所以提出这一点,是因为它是标准语法,并且与您的查询版本有关(并且受相关数据库的支持)。

相关问题