如何选择最佳客户并通过他们支付的数量进行分组
我正在使用SQL Server选择此表,并根据他们向商店的总付款进行分组和名字。我尝试使用,
select custid, name, sum(payment)
from transactions
group by custid
order by payment desc
但它返回以下错误:
均不包含在汇总函数中,或者由子句
中包含
您可以澄清我做错了什么吗?
这是我的客户表
Cust ID | 付款 | 名称 |
---|---|---|
T001 | 60 | Katy |
T002 | 12 | Amy |
T003 | 40 | Leon |
T001 | 20 | Katy |
这是结果表,我想获得
Cust ID | 付款 | 名称 |
---|---|---|
T001 | 80 | KATY |
T003 | 12 | AMY |
T002 | 40 | LEON |
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 id | payment | name |
---|---|---|
T001 | 60 | katy |
T002 | 12 | amy |
T003 | 40 | leon |
T001 | 20 | katy |
this is the result table I want to get
cust id | payment | name |
---|---|---|
T001 | 80 | katy |
T003 | 12 | amy |
T002 | 40 | leon |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误是指 name 或付款列,这两种都不汇总。
您需要按相同的表达式订购 order order ,或为列提供一个别名;除了列名称不一致之外,请尝试:
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:
您的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.