SELECT 具有多个组和总和的数据字段

发布于 2025-01-01 11:37:01 字数 730 浏览 1 评论 0原文

我似乎无法按多个数据字段进行分组并对特定的分组列求和。

我想将人员分组到客户,然后将客户分组到价格,然后将价格相加。总和(价格)最高的人应按升序列出。

示例:

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

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

发布评论

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

评论(3

魔法唧唧 2025-01-08 11:37:01

我可以看到两个问题。一种是有点挑剔,另一种是相当基础的。

用 SQL 表示数据

SQL 返回表格数据集。它无法返回带有标题的子集,类似于数据透视表。

这意味着这是不可能的...

**BOB:** 
Orange, $2230

**GREG:**
green,  $360
blue,   $170

但是这是可能的...

Bob,  Orange, $2230
Greg, Green,  $360
Greg, Blue,   $170

关联数据

我可以直观地看到如何将数据关联在一起...

table customer                 table invoice
--------------                 -------------
customer | common_id           person | price |common_id
 green        2                 greg     360       2
 blue         2                 greg     170       2
 orange       1                 bob     2330       1

但是 SQL 没有任何隐含的顺序。只有当一个表达式可以表明事物是相关的时,事物才能相关。例如,以下情况同样可能...

table customer                 table invoice
--------------                 -------------
customer | common_id           person | price |common_id
 green        2                 greg     170       2      \ These two have 
 blue         2                 greg     360       2      / been swapped
 orange       1                 bob     2330       1

这意味着您需要规则(以及可能的附加字段)来明确说明哪个 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...

**BOB:** 
Orange, $2230

**GREG:**
green,  $360
blue,   $170

But that this is possible...

Bob,  Orange, $2230
Greg, Green,  $360
Greg, Blue,   $170

Relating data

I can visually see how you relate the data together...

table customer                 table invoice
--------------                 -------------
customer | common_id           person | price |common_id
 green        2                 greg     360       2
 blue         2                 greg     170       2
 orange       1                 bob     2330       1

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...

table customer                 table invoice
--------------                 -------------
customer | common_id           person | price |common_id
 green        2                 greg     170       2      \ These two have 
 blue         2                 greg     360       2      / been swapped
 orange       1                 bob     2330       1

This means that you need rules (and likely additional fields) that explicitly state which customer record matches which invoice record, especially when there are multiples in both with the same common_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 for common_id = 2, but only two records in invoice for common_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.

椒妓 2025-01-08 11:37:01

您应该使用除 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

赠意 2025-01-08 11:37:01

我不确定你怎么可能做到这一点。 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.

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