SELECT 具有多个组和总和的数据字段
我似乎无法按多个数据字段进行分组并对特定的分组列求和。
我想将人员分组到客户,然后将客户分组到价格,然后将价格相加。总和(价格)最高的人应按升序列出。
示例:
table customer
-----------
customer | common_id
green 2
blue 2
orange 1
table invoice
----------
person | price | common_id
bob 2330 1
greg 360 2
greg 170 2
SELECT DISTINCT
min(person) As person,min(customer) AS customer, sum(price) as price
FROM invoice a LEFT JOIN customer b ON a.common_id = b.common_id
GROUP BY customer,price
ORDER BY person
我想要的结果是:
**BOB:**
Orange, $2230
**GREG:**
green, $360
blue,$170
The colors are the customer, that GREG and Bob handle. Each color has a price.
I cant seem to group by multiple data fields and sum a particular grouped column.
I want to group Person to customer and then group customer to price and then sum price. The person with the highest combined sum(price) should be listed in ascending order.
Example:
table customer
-----------
customer | common_id
green 2
blue 2
orange 1
table invoice
----------
person | price | common_id
bob 2330 1
greg 360 2
greg 170 2
SELECT DISTINCT
min(person) As person,min(customer) AS customer, sum(price) as price
FROM invoice a LEFT JOIN customer b ON a.common_id = b.common_id
GROUP BY customer,price
ORDER BY person
The results I desire are:
**BOB:**
Orange, $2230
**GREG:**
green, $360
blue,$170
The colors are the customer, that GREG and Bob handle. Each color has a price.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以看到两个问题。一种是有点挑剔,另一种是相当基础的。
用 SQL 表示数据
SQL 返回表格数据集。它无法返回带有标题的子集,类似于数据透视表。
这意味着这是不可能的...
但是这是可能的...
关联数据
我可以直观地看到如何将数据关联在一起...
但是 SQL 没有任何隐含的顺序。只有当一个表达式可以表明事物是相关的时,事物才能相关。例如,以下情况同样可能...
这意味着您需要规则(以及可能的附加字段)来明确说明哪个
customer
记录与哪个invoice
记录匹配,尤其是当两者中有多个具有相同的common_id
。规则的一个示例是,最低价格始终与按字母顺序排列的第一个客户匹配。但是,如果您在
customer
中有common_id = 2
的三条记录,但只有,会发生什么情况?发票
中有common_id = 2
的两条条记录?或者记录数量是否始终匹配,并且您强制执行这一点吗?您很可能需要一条(或多条)额外信息来了解哪些记录彼此相关。
There are two issues that I can see. One is a bit picky, and one is quite fundamental.
Presentation of data in SQL
SQL returns tabular data sets. It's not able to return sub-sets with headings, looking something a Pivot Table.
The means that this is not possible...
But that this is possible...
Relating data
I can visually see how you relate the data together...
But SQL doesn't have any implied ordering. Things can only be related if an expression can state that they are related. For example, the following is equally possible...
This means that you need rules (and likely additional fields) that explicitly state which
customer
record matches whichinvoice
record, especially when there are multiples in both with the samecommon_id
.An example of a rule could be, the lowest price always matches with the first customer alphabetically. But then, what happens if you have three records in
customer
forcommon_id = 2
, but only two records ininvoice
forcommon_id = 2
? Or do the number of records always match, and do you enforce that?Most likely you need an extra piece (or pieces) of information to know which records relate to each other.
您应该使用除 sum 之外的所有选定字段进行分组,那么函数 group_concat (mysql) 可以帮助您连接 group 子句的结果行
you should group by using all your selected fields except sum then maybe the function group_concat (mysql) can help you in concatenating resulting rows of the group clause
我不确定你怎么可能做到这一点。 Greg 有 2 种颜色和 2 种价格,您如何确定哪种搭配?
格雷格·布鲁 170 或格雷格·布鲁 360 ????或者将绿色附加到任一价格上?
我认为颜色需要有唯一的标识符,与人的唯一标识符分开。
只是一个想法。
Im not sure how you could possibly do this. Greg has 2 colors, AND 2 prices, how do you determine which goes with which?
Greg Blue 170 or Greg Blue 360 ???? or attaching the Green to either price?
I think the colors need to have unique identofiers, seperate from the person unique identofiers.
Just a thought.