如何选择最佳客户并通过他们支付的数量进行分组

发布于 2025-02-04 20:36:21 字数 908 浏览 3 评论 0原文

我正在使用SQL Server选择此表,并根据他们向商店的总付款进行分组和名字。我尝试使用,

select custid, name, sum(payment)
from transactions
group by custid
order by payment desc

但它返回以下错误:

均不包含在汇总函数中,或者由子句

中包含

您可以澄清我做错了什么吗?

这是我的客户表

Cust ID付款名称
T00160Katy
T00212Amy
T00340Leon
T00120Katy

这是结果表,我想获得

Cust ID付款名称
T00180KATY
T00312AMY
T00240LEON

I am using SQL server to select this table and group the customer id and first name according to their total payments made to the store. I tried using

select custid, name, sum(payment)
from transactions
group by custid
order by payment desc

but it returns the following error:

not contained in either an aggregate function or the GROUP BY clause

Could you please clarify what I did wrong?

this is my customer table

cust idpaymentname
T00160katy
T00212amy
T00340leon
T00120katy

this is the result table I want to get

cust idpaymentname
T00180katy
T00312amy
T00240leon

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

不及他 2025-02-11 20:36:21

错误是指 name 付款列,这两种都不汇总。

您需要按相同的表达式订购 order order ,或为列提供一个别名;除了列名称不一致之外,请尝试:

select Cust_Id, sum(Payment) as TotalPayment, Name
from transactions
group by Cust_Id, Name
order by TotalPayment desc;

The error refers to the name or payment columns, both of which you refer to without aggregating.

You need to order by the same expression or provide the column an alias; column name inconsistencies aside, try:

select Cust_Id, sum(Payment) as TotalPayment, Name
from transactions
group by Cust_Id, Name
order by TotalPayment desc;
苍景流年 2025-02-11 20:36:21

您的Cust ID列中有一个空间。您的代码不包含空间。尝试使用Cust和ID之间的空间。

There's a space in your cust id column. Your code does not contain the space. Try it with a space between cust and id.

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