Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 具有max的子查询_Sql - Fatal编程技术网

Sql 具有max的子查询

Sql 具有max的子查询,sql,Sql,为什么这里有错误?如何修复?它表示缺少表达式。整个select max()…part返回一个值,但在where子句之后,应该有类似where something=1的表达式。 我只能猜测您希望通过此查询实现什么,但可能应该是这样的: select substr(phone,0,3) as area_code from customer where (select max(count(area_code)) from customer); 注意:如果您想要一个最大用户数的错误

为什么这里有错误?如何修复?它表示缺少表达式。

整个
select max()…
part返回一个值,但在
where
子句之后,应该有类似
where something=1的表达式。
我只能猜测您希望通过此查询实现什么,但可能应该是这样的:

select substr(phone,0,3) as area_code
from customer
where (select max(count(area_code)) 
         from customer);

注意:如果您想要一个最大用户数的错误代码,那么您通常会将其表述为:

select substr(phone,0,3) as area_code from customer 
where area_code in (select max(substr(phone,0,3)) from customer);
窗口函数也常用于此:

select substr(phone, 1, 3) as area_code
from customer
group by substr(phone, 1, 3) 
order by count(*) desc
fetch first 1 row only;

请注意,
rank()
将返回多行,如果最常见的区号有关联。

Hi!请回答您的问题,并为您正在使用的数据库(MySQL、Oracle、PostgreSQL、MS SQL Server?)添加标签。另外,在编辑您的问题时,请向我们展示一些示例数据,以了解为什么它会给您带来错误。这样我们可以更好地帮助你。请毫不犹豫地浏览或中的,以确定如何提问。此外,在WHERE中有一个
SELECT MAX…
,而不将结果与其他内容进行比较,这似乎很奇怪。你想用WHERE做什么?WHERE中缺少Yes表达式。不清楚你想做什么。@samlee。请解释一下逻辑应该做什么。非工作SQL通常不能很好地解释这一点。
where
子句需要一个布尔表达式,但您的子选择不返回布尔值,而是返回一个数字。您可能希望在where子句中重复“substr(phone,0,3)”而不是区域代码,因为您通常无法在where子句中直接引用别名。
select area_code
from (select substr(phone, 1, 3) as area_code,
             rank() over (order by count(*) desc) as seqnum
      from customer
      group by substr(phone, 1, 3) 
     ) t
where seqnum = 1;