0

我对 Oracle 上的 EXTRACT 函数有疑问,我不知道下面的脚本有什么问题。出于某种原因,如果我将 AS 关键字与 EXTRACT 一起使用,它会在 GROUP BY 语句中引用时抛出 Invalid Identifier。如果我通过没有 AS 关键字给出的自动名称引用该列,它会起作用。

WITH counted AS(
  SELECT UNIT,
         STATUS,
         SDESC,
         COUNT(1) AS cnt,
         EXTRACT(month FROM STATUS_DATE) AS statusMonth,
         ROW_NUMBER() OVER(PARTITION BY UNIT ORDER BY COUNT(1) DESC) AS rk
  FROM ATMSTATUS
  WHERE 
    STATUS_DATE BETWEEN '1-OCT-13' AND '31-OCT-13' AND 
    STATUS > 0
  GROUP BY UNIT, STATUS, SDESC, statusMonth
  ORDER BY UNIT, cnt DESC
)
SELECT *
FROM counted
WHERE rk < (10 + 1)
4

1 回答 1

1

您不能在GROUP BY子句中使用别名,而必须使用整个表达式:

WITH counted AS(
  SELECT UNIT,
         STATUS,
         SDESC,
         COUNT(1) AS cnt,
         EXTRACT(month FROM STATUS_DATE) AS statusMonth,
         ROW_NUMBER() OVER(PARTITION BY UNIT ORDER BY COUNT(1) DESC) AS rk
  FROM ATMSTATUS
  WHERE 
    STATUS_DATE BETWEEN '1-OCT-13' AND '31-OCT-13' AND 
    STATUS > 0
  GROUP BY UNIT, STATUS, SDESC, EXTRACT(month FROM STATUS_DATE)
  ORDER BY UNIT, cnt DESC
)
SELECT *
FROM counted
WHERE rk < (10 + 1)
于 2013-11-05T15:01:14.820 回答