ORA-00907“缺少右括号”的摘录年月

时间:2018-08-13 00:25:35

标签: sql oracle

今天所有的一切都过得很恐怖。我正在尝试获取月,年的列表,然后平均每个月的订单总数。我收到ORA-00907“缺少正确的括号”,但我不确定为什么。同样,我对此很陌生,但是我从https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm引用的代码非常感谢。

SELECT EXTRACT (MONTH, YEAR FROM ORDERDATE) "DATE"
AVG (ORDERDATE) "NO. OF ORDERS"
FROM ORDERINFO
GROUP BY EXTRACT (MONTH, YEAR FROM ORDERDATE)
ORDER BY "MONTH" ASC; 

3 个答案:

答案 0 :(得分:4)

您引用的文档显示,只有一个日期部分说明符可以具有提取功能。

例如extract(month from orderdate)extract(year from orderdate)

我猜您真的想截断订单日期。参见https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions201.htm

SELECT trunc(orderdate, 'MONTH') AS "date"

答案 1 :(得分:3)

要么:

select
  extract(month from orderdate) as year,
  extract(year from orderdate) as month,
  ...
from ...
group by extract(month from orderdate), extract(year from orderdate)

或:

select
  to_char(orderdate, 'YYYY-MM') as year_month,
  ...
from ...
group by to_char(orderdate, 'YYYY-MM')

答案 2 :(得分:-1)

这将起作用:

select extract(month from orderdate) "MONTH",
       extract(year from orderdate) "YEAR",
from orderdate
group by extract(month from orderdate)
order by extract(month from orderdate) asc;