提取(从日期开始)问题

时间:2013-11-05 14:47:41

标签: oracle plsql

我在Oracle上有EXTRACT函数的问题,我不知道下面的脚本有什么问题。出于某种原因,如果我在EXTRACT中使用AS关键字,它会在GROUP BY语句中引用时抛出无效标识符。如果我通过自动名称引用列,它没有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)

1 个答案:

答案 0 :(得分: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)