我有一个名为CUSTOMERS
的表,其中有一个列COUNTRY
。我想检索表COUNTRY
中顾客最多的城市,换句话说就是CUSTOMERS
中最频繁的城市。
我收到如下错误消息:
ORA-00904:“国家/地区”:无效的标识符
我的代码:
SELECT
COUNTRY,
COUNT(COUNTRY) AS `value_occurrence`
FROM
CUSTOMERS
GROUP BY
COUNTRY
ORDER BY
`value_occurrence` DESC
LIMIT 1;
答案 0 :(得分:2)
您的语法是MySQL,不可移植。 MySQL是Oracle以外的另一个DBMS。
这是您在标准SQL中的查询。从12c版开始,它就可以在Oracle中使用。
select country, count(*) as value_occurrence
from customers
order by value_occurrence desc
fetch first row only;
在早期的Oracle版本中,您可以使用:
select country, value_occurrence
from
(
select
country,
count(*) as value_occurrence,
row_number() over (order by count(*) desc) as rn
from customers
)
where rn = 1;
如果您想允许平局,则可以在第一个查询中将only
更改为with ties
,将其中的row_number
更改为rank
或dense_rank
第二个。