Sql 使用coalesce()避免嵌套聚合错误
我目前有一个在SQL server中使用coalesce的查询,但是,它在Amazon Redshift中不起作用。有没有一种方法可以更恰当地在红移中使用:Sql 使用coalesce()避免嵌套聚合错误,sql,null,aggregate,amazon-redshift,coalesce,Sql,Null,Aggregate,Amazon Redshift,Coalesce,我目前有一个在SQL server中使用coalesce的查询,但是,它在Amazon Redshift中不起作用。有没有一种方法可以更恰当地在红移中使用: coalesce(sum(Score)/nullif(sum(ScorePrem),0),0) as percent 考虑将聚合查询作为子查询或CTE运行,然后在外部主查询中处理转换或辅助计算 WITH agg AS ( SELECT calendar_month_id ,day_of_month
coalesce(sum(Score)/nullif(sum(ScorePrem),0),0) as percent
考虑将聚合查询作为子查询或CTE运行,然后在外部主查询中处理转换或辅助计算
WITH agg AS (
SELECT calendar_month_id
,day_of_month
,month_name
,DaysRemaining
,RPTBRANCH
,0 AS TotalGrp
,SUM(Score) AS Score
,SUM(ScorePrem) AS ScorePrem
FROM #temp_Score
GROUP BY calendar_month_id
, day_of_month
, month_name
, DaysRemaining
, RPTBranch
)
SELECT calendar_month_id
,day_of_month
,month_name
,DaysRemaining
,RPTBRANCH
,TotalGrp
,Score
,ScorePrem
,COALESCE(Score/NULLIF(ScorePrem,0),0) AS percent
FROM agg
将
nullif
替换为coalesce
。您得到的结果有什么问题?如果您遇到错误,请共享实际的错误消息。@这表明不允许嵌套聚合调用。我试着替换nullif,但也没用。有什么想法吗?@a_horse_没有名字请查看编辑后的帖子。这是PostgreSQL 8.0.2和Redshift 1.0.16496的版本。好吧,Redshift不是Postgres。只是好奇我的select和它一起会是什么样子?我似乎无法让它工作。谢谢你的帮助!将原始查询放在CTE中,然后使用最终表达式选择所需的列。请参见编辑。