简体   繁体   English

列 'City.Name' 在 select 列表中无效,因为它不包含在聚合 function 或 GROUP BY 子句中

[英]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.

相关问题 选择列表中的“ city.POPULATION”列无效,因为它既不在聚合函数中也不在GROUP BY子句中 - Column 'city.POPULATION' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 列 'seller.seller_Name' 在 select 列表中无效,因为它不包含在聚合 function 或 GROUP BY 子句中 - Column 'seller.seller_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 选择列表中的“ Course.Course_Name”列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中 - Column 'Course.Course_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 在选择列表中,列“ tbl_Stock.Item_Name”无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中 - Column 'tbl_Stock.Item_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 列“Users.Name”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中 - Column 'Users.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 列“faculty.f_name”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中 - Column 'faculty.f_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 选择列表中的“ Product.product_name”列无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中 - Column 'Product.product_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 柱 <name> 在选择列表中无效,因为它未包含在聚合函数或GROUP BY子句中 - Column <name> is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中 - invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause GROUP BY给出“列在选择列表中无效,因为该列未包含在聚合函数或GROUP BY子句中” - GROUP BY gives “Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM