这个查询语句怎么写呢?
我想按名称检索所有信息以及金额总和,因此我使用此查询来实现此目的。
select SUM(Amount)as total,RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other from Amount where Name='Shaikh'
但发生了这个错误。 列“Amount.RecieptNo”在选择列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中。 所以请帮助我。
I want to retrieve all information as well as sum of amount by the name so i use this query for that purpose.
select SUM(Amount)as total,RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other from Amount where Name='Shaikh'
but this error is occured.
Column 'Amount.RecieptNo' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
so please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SQL Server 2005+ 中提供的另一个选择:
Another choice available in SQL Server 2005+:
使用聚合 SQL 函数(例如 SUM())时,必须使用 GROUP BY 子句。下面的例子证明了这一点。然而,这可能不是解决您的查询的最理想方法。我所做的是添加一个 group by 子句,其中包括您选择的除 SUM 之外的所有列。例如:
When using aggregate SQL functions such as SUM(), you have to use a GROUP BY clause. The example below demonstrates that. This may not be the most ideal approach to tackling your query however. What I've done is added a group by clause that includes all columns that you've selected other than your SUM. For example:
您需要分组
如果您想在选择列表中添加其他内容,您需要在分组依据子句中添加此字段,否则会出现错误
You need to group by
If you want to add other thing in select list you require to add this field in you group by clause also otherwise it will give error