最大子查询

时间:2018-09-03 22:37:24

标签: sql

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

为什么这里有错误?怎么修?它说缺少表达。.

2 个答案:

答案 0 :(得分:0)

整个select max() ...部分返回一个值,但是在where子句之后,您应该具有类似where something = 1的表达式。 我只能猜测您要使用此查询实现什么,但也许看起来应该像这样:

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

P.S。看here

答案 1 :(得分:0)

如果您要使用最多用户数的错误代码,则通常将其表达为:

public string UserName { get; set; }
public string Password { get; set; }

窗口函数通常也用于此:

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;

请注意,如果最常见的区号有联系,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; 将返回多行。