Avoid nested aggregate error using coalesce()

前端 未结 1 1574
旧巷少年郎
旧巷少年郎 2021-01-27 05:29

I currently have a query using coalesce that worked in SQL server,however, it is not working in Amazon Redshift. Is there a way I can more appropriately write this to use in Red

相关标签:
1条回答
  • 2021-01-27 06:08

    Consider running the aggregate query as a subquery or CTE, then handle transformation or secondary calculations in an outer main query.

    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
    
    0 讨论(0)
提交回复
热议问题