[英]Column 'City.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
I tried the following SQL Query and it is giving the error stated below:我尝试了以下 SQL 查询,它给出了以下错误:
Select TOP 1 CITY,MAX(LEN(CITY))
from STATION
ORDER BY CITY ASC
Error错误
Column 'STATION.CITY' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
列 'STATION.CITY' 在 select 列表中无效,因为它不包含在聚合 function 或 GROUP BY 子句中。
Table:桌子:
CREATE TABLE City (
ID Integer NOT NULL,
PRIMARY KEY (ID),
Name VARCHAR (255),
CountryCode VARCHAR (255),
District VARCHAR (255),
PopulationofCity Integer
);
you have to city in group by你必须按组城市
Select TOP 1 CITY,MAX(LEN(CITY))
from STATION group by CITY
ORDER BY CITY ASC
You are missing group by clause您缺少 group by 子句
Select TOP 1 CITY,MAX(LEN(CITY)) from STATION
group by city
ORDER BY CITY ASC
You probably are looking for the longest city name.您可能正在寻找最长的城市名称。
Then order by descending length然后按长度降序排列
SELECT TOP 1 CITY, LEN(CITY)
FROM STATION
GROUP BY CITY
ORDER BY LEN(CITY) DESC
Will this help?这会有帮助吗?
SELECT TOP 1 * FROM (
SELECT DISTINCT CITY,LEN(CITY) AS CITYLENGTH FROM STATION ) A ORDER BY A.CITYLENGTH DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.