Excel表中的SQL查询

问题描述 投票:0回答:2

我在Excel中的数据库中使用OleDbCommand运行查询,包含以下列:

name, city, inhabitants

我的代码:

 Cmd = new OleDbCommand();
 Cmd.Connection = Conn;
 Cmd.Parameters.AddWithValue("@city", city);
 Cmd.CommandText = "Select City, count(habitants) as h from [Sheet1$] where city = @city group by city";
 var Reader = await Cmd.ExecuteReaderAsync();

我收到此错误:

OleDbException:您尝试执行不包含指定表达式“city”的查询作为聚合函数的一部分。

如果它包含city列,为什么会出现此错误?

sql select
2个回答
0
投票

您可以通过在查询中添加GROUP BY来解决此错误:

Cmd.CommandText = "Select City, count(habitants) as h from [Sheet1$] where city=@city group by City";

0
投票

使用FIRST()函数为city

Cmd.CommandText = "SELECT FIRST(city) AS city, COUNT(habitants) AS h FROM [Sheet1$] WHERE city=@city GROUP BY city";
© www.soinside.com 2019 - 2024. All rights reserved.