重新分组数据以便求和

huangapple go评论64阅读模式
英文:

Regroup data before sum

问题

我对SQL查询是个新手,但我做了这个简单的查询,它输出了以下数据:

    select ID, Length, Weight
    from MyTable
    where Group in('A1', 'A2')

我想要做的是按ID和Length分组,然后只计算Weight列中所有数据的总和。

基本上我会得到:

| ID       | Length   | Weight   |
| -------- | -------- | -------- |
| 1        | 2,635    | 2,2      |
| 1        | 2,935    | 43,8     |
| 2        | 2,305    | 1        |
| 2        | 2,935    | 45       |
| 3        | 0,281    | 0,1      |
| 3        | 0,443    | 0,2      |
| 3        | 0,604    | 0,3      |

然后返回Weight列的总和。

听起来很简单,但我无法让它工作...

有任何想法吗?
谢谢您提前。
英文:

I'm a beginner with SQL Queries but I made this simple query which output the following data:

select ID, Length, Weight
from MyTable
where Group in('A1', 'A2')

重新分组数据以便求和

What I would like to do is to group the lines by ID and Length and then only sum of all the data in the Weight column.

So basically I would get :

ID Length Weight
1 2,635 2,2
1 2,935 43,8
2 2,305 1
2 2,935 45
3 0,281 0,1
3 0,443 0,2
3 0,604 0,3

and then return the sum of the weight column.

This sounds simple but I can't manage to make it work...

Any Idea ?
Thank you in advance

答案1

得分: 1

假设长度 0.604 在你的问题描述中表示 0.604,可以使用 group by 操作符和 sum() 函数来得到你想要的结果,然后使用 SQL window function 将所有权重总和添加到每一行。

请参考以下代码示例:

select ID, Lenght, 
sum(Weight) as toal_weight_per_line,
SUM(SUM(weight)) OVER() as all_weight
from record
GROUP BY ID, Lenght
ORDER BY ID, Lenght

已验证运行示例 SQLFIDDLE 链接

ID Lenght toal_weight_per_line all_weight
1 2.635 2.2 92.6
1 2.935 43.8 92.6
2 2.305 1 92.6
2 2.935 45 92.6
3 0.281 0.1 92.6
3 0.443 0.2 92.6
3 0.604 0.3 92.6

另一种方法是使用 CROSS JOIN 操作符。

请参考以下代码示例:

SELECT
    per_line.ID,
    per_line.Lenght,
    per_line.toal_weight_per_line,
    total.all_weight
FROM
(
    select ID, Lenght, 
    sum(Weight) as toal_weight_per_line
    from record
    GROUP BY ID, Lenght
) per_line
cross join
(
    select sum(weight) as all_weight
    FROM
    record
) total
ORDER BY ID, Lenght

已验证运行示例 SQLFIDDLE 链接

ID Lenght toal_weight_per_line all_weight
1 2.635 2.2 92.6
1 2.935 43.8 92.6
2 2.305 1 92.6
2 2.935 45 92.6
3 0.281 0.1 92.6
3 0.443 0.2 92.6
3 0.604 0.3 92.6
英文:

Assume lenght 0,604 mean 0.604 in your problem description, a group by operator and sum() function should be able to get what your want, and then use SQL window function to all weight sum added to each line.

See the following code as an example:

select ID, Lenght, 
sum(Weight) as toal_weight_per_line,
SUM(SUM(weight)) OVER() as all_weight
from record
GROUP BY ID, Lenght
ORDER BY ID, Lenght

VERIFIED RUNNING EXAMPLE SQLFIDDLE LINK

ID Lenght toal_weight_per_line all_weight
1 2.635 2.2 92.6
1 2.935 43.8 92.6
2 2.305 1 92.6
2 2.935 45 92.6
3 0.281 0.1 92.6
3 0.443 0.2 92.6
3 0.604 0.3 92.6

Another method is to use CROSS JOIN operator

See the following code as example:

SELECT
    per_line.ID,
    per_line.Lenght,
    per_line.toal_weight_per_line,
    total.all_weight
FROM
(
    select ID, Lenght, 
    sum(Weight) as toal_weight_per_line
    from record
    GROUP BY ID, Lenght
) per_line
cross join
(
    select sum(weight) as all_weight
    FROM
    record
) total
ORDER BY ID, Lenght

VERIFIED RUNNING EXAMPLE SQLFIDDLE LINK

ID Lenght toal_weight_per_line all_weight
1 2.635 2.2 92.6
1 2.935 43.8 92.6
2 2.305 1 92.6
2 2.935 45 92.6
3 0.281 0.1 92.6
3 0.443 0.2 92.6
3 0.604 0.3 92.6

答案2

得分: 0

这是否解决了您的问题?

选择 ID,长度,SUM(重量) 作为 总重量
 我的表格
 分组  ('A1', 'A2')
 ID,长度 分组
英文:

Does this answer your problem?

SELECT ID, Length, SUM(Weight) AS TotalWeight
FROM MyTable
WHERE Group IN ('A1', 'A2')
GROUP BY ID, Length

huangapple
  • 本文由 发表于 2023年2月23日 19:19:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定