这个查询语句怎么写呢?

发布于 2024-11-03 09:46:17 字数 257 浏览 1 评论 0原文

我想按名称检索所有信息以及金额总和,因此我使用此查询来实现此目的。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

滿滿的愛 2024-11-10 09:46:17
Select RecieptNo, Name
    , UniqueId, Date
    , Amount
    , LateFee, Other 
    , ( Select Sum( A1.Amount )
        From Amount As A1
        Where A1.Name = A.Name ) As Total
From Amount As A
Where Name='Shaikh'

SQL Server 2005+ 中提供的另一个选择:

Select RecieptNo, Name
    , UniqueId, Date
    , Amount
    , LateFee, Other 
    , Sum( Amount ) Over( Partition By Name ) As Total
From Amount As A
Where Name='Shaikh'
Select RecieptNo, Name
    , UniqueId, Date
    , Amount
    , LateFee, Other 
    , ( Select Sum( A1.Amount )
        From Amount As A1
        Where A1.Name = A.Name ) As Total
From Amount As A
Where Name='Shaikh'

Another choice available in SQL Server 2005+:

Select RecieptNo, Name
    , UniqueId, Date
    , Amount
    , LateFee, Other 
    , Sum( Amount ) Over( Partition By Name ) As Total
From Amount As A
Where Name='Shaikh'
白云悠悠 2024-11-10 09:46:17
select SUM(Amount)as total,RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other from Amount
group by RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other having Name='Shaikh'
select SUM(Amount)as total,RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other from Amount
group by RecieptNo,Name,UniqueId,Date,Amount,LateFee,Other having Name='Shaikh'
初与友歌 2024-11-10 09:46:17

使用聚合 SQL 函数(例如 SUM())时,必须使用 GROUP BY 子句。下面的例子证明了这一点。然而,这可能不是解决您的查询的最理想方法。我所做的是添加一个 group by 子句,其中包括您选择的除 SUM 之外的所有列。例如:

    select RecieptNo, Total, UniqueId, Date, Amount, LateFee, Other SUM(Amount)as total  from 
Amount where Name='Shaikh' group by RecieptNo, Total, UniqueId, Date, Amount, LateFee, Other

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:

    select RecieptNo, Total, UniqueId, Date, Amount, LateFee, Other SUM(Amount)as total  from 
Amount where Name='Shaikh' group by RecieptNo, Total, UniqueId, Date, Amount, LateFee, Other
二手情话 2024-11-10 09:46:17

您需要分组

select SUM(Amount)as total,Name from Amount where Name='Shaikh' group by Name

如果您想在选择列表中添加其他内容,您需要在分组依据子句中添加此字段,否则会出现错误

You need to group by

select SUM(Amount)as total,Name from Amount where Name='Shaikh' group by Name

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文