0

我正在尝试在 postgresql 中运行此查询,但收到此错误:“错误:聚合函数调用可能未嵌套”。我不确定这是什么问题或解决方案。

这是我的查询:

select a.KEY_NBR, a.MAIN_ID
        , 
            case
                when a.KEY_NBR = a.MAIN_ID
                    then 'Don''t Use'
                when a.count = MAX(a.count) over(partition by a.KEY_NBR)
                    then 'Good'
                else 'Bad'
            end [flag]
    from MYTABLE a
4

1 回答 1

2

您的查询看起来不错,应该可以工作。也许您使用的版本有一些限制。试试这个变化:

WITH a AS
  ( SELECT key_nbr, main_id, count,
           MAX(count) OVER (PARTITION BY key_nbr) AS max_count
    FROM mytable
  ) 
SELECT a.key_nbr, a.main_id,
       CASE WHEN a.key_nbr = a.main_id
                THEN 'Don''t Use'
            WHEN a.count = max_count 
                THEN 'Good'
            ELSE 'Bad'
       END AS flag
FROM a ;
于 2013-10-23T07:21:00.380 回答