Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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
Sql 使用coalesce()避免嵌套聚合错误_Sql_Null_Aggregate_Amazon Redshift_Coalesce - Fatal编程技术网

Sql 使用coalesce()避免嵌套聚合错误

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

我目前有一个在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
         ,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中,然后使用最终表达式选择所需的列。请参见编辑。