多个表格上的分组求和

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

multiple sums on grouped tables

问题

表头
+---------+-------+
| ref     | total |
+---------+-------+
| 2544    |  2000 |
| 2544    |    10 |
| 2545    |  2566 |
| 2545    |    34 |
+---------+-------+

表项
+---------+-------+
| ref     | total |
+---------+-------+
| 2544    |  1500 |
| 2544    |   500 |
| 2545    |  2000 |
| 2545    |   500 |
| 2545    |   100 |
+---------+-------+

我想要对两个表中的总计列按ref分组求和,所以我的输出应该类似于:

输出:
+---------+--------------+-------------+
| ref     | total_header | total_items | 
+---------+--------------+-------------+
| 2544    |        2010  |       2000  |
| 2545    |        2600  |       2600  |
+---------+--------------+-------------+

然后我可以比较两个总计列并检查差异。

查询:

选择
    表头.ref,
    求和(表头.total) 作为 total_header
    表头
分组按
    表头.ref
    表头.ref 降序排序

这给我提供了表头表的正确信息。但是,一旦我将表项表添加到查询中,total_header 和 total_items 就会给出错误的值。

以下是我使用的查询....

选择
    表头.ref,
    求和(表头.total) 作为 total_header,
    求和(表项.total) 作为 total_items
    表头 左连接
    表项 On 表头.ref = 表项.ref
分组按
    表头.ref
    表头.ref 降序排序

有人能帮助我创建一个可以生成上述输出的查询吗?此外,如果可能的话,还可以使用另一列来比较这两个总计列,根据它们是否匹配来给出 true 或 false。

谢谢,

Martin。
英文:
Table Header
+---------+-------+
| ref     | total | 
+---------+-------+
| 2544    |  2000 |
| 2544    |    10 |
| 2545    |  2566 |
| 2545    |    34 |
+---------+-------+

Table Items
+---------+-------+
| ref     | total | 
+---------+-------+
| 2544    |  1500 |
| 2544    |   500 |
| 2545    |  2000 |
| 2545    |   500 |
| 2545    |   100 |
+---------+-------+

I would like to sum the total column in both tables grouped by ref, so my output would look something like:

Output:
+---------+--------------+-------------+
| ref     | total_header | total_items | 
+---------+--------------+-------------+
| 2544    |        2010  |       2000  |
| 2545    |        2600  |       2600  |
+---------+--------------+-------------+

I can then compare the two total columns and check for differences.

The query:

Select
    Header.ref,
    Sum(Header.total) as total_header
From
    Header
Group By
    Header.ref
Order By
    Header.ref Desc

This gives me the correct info for the Header table. As soon as I add the Items table to the query then the total_header and total_items give the wrong values

Here is the query that I am using....

Select
    Header.ref,
    Sum(Header.total) as total_header,
    Sum(Items.total) as total_items
From
    Header Left Join
    Items On Header.ref = Items.ref
Group By
    Header.ref
Order By
    Header.ref Desc

Can anyone help me create a query that would produce the output above? Also, if possible with another column that compares the 2 total columns giving true or false depending on if they match.

Thank you,

Martin.

答案1

得分: 1

以下是翻译好的部分:

你可以首先获取总和,然后将它们连接 - 

SELECT H.ref, H_TOT, I_TOT
FROM (SELECT ref, SUM(total) H_TOT
      FROM Header
      GROUP BY ref) H
JOIN (SELECT ref, SUM(total) I_TOT
      FROM Items
      GROUP BY ref) I ON H.ref = I.ref
ORDER BY H.ref
英文:

You may first get the sum and then join them -

SELECT H.ref, H_TOT, I_TOT
FROM (SELECT ref, SUM(total) H_TOT
      FROM Header
      GROUP BY ref) H
JOIN (SELECT ref, SUM(total) I_TOT
      FROM Items
      GROUP BY ref) I ON H.ref = I.ref
ORDER BY H.ref

答案2

得分: 0

使用UNION ALL连接两个表,然后进行聚合:

select t.ref,
  sum(t.total_header) total_header,
  sum(t.total_items) total_items,
  sum(t.total_header) = sum(t.total_items) matching
from (  
  select ref, total total_header, 0 total_items from Header
  union all
  select ref, 0 total_header, total total_items from Items
) t  
group by t.ref

查看演示。<br/>
结果:

| ref  | total_header | total_items | matching |
| ---- | ------------ | ----------- | -------- |
| 2544 | 2010         | 2000        | 0        |
| 2545 | 2600         | 2600        | 1        |
英文:

With UNION ALL of the 2 tables and then aggregate:

select t.ref,
  sum(t.total_header) total_header,
  sum(t.total_items) total_items,
  sum(t.total_header) = sum(t.total_items) matching
from (  
  select ref, total total_header, 0 total_items from Header
  union all
  select ref, 0 total_header, total total_items from Items
) t  
group by t.ref

See the demo.<br/>
Results:

| ref  | total_header | total_items | matching |
| ---- | ------------ | ----------- | -------- |
| 2544 | 2010         | 2000        | 0        |
| 2545 | 2600         | 2600        | 1        |

答案3

得分: 0

可能的解决方案

SELECT refs.ref, sum_header.header_total, sum_items.items_total
FROM ( SELECT ref FROM header
       UNION
       SELECT ref FROM items ) refs
LEFT JOIN ( SELECT ref, SUM(total) header_total
            FROM header
            GROUP BY ref ) sum_header USING (ref)
LEFT JOIN ( SELECT ref, SUM(total) items_total
            FROM items
            GROUP BY ref ) sum_items USING (ref)

这个解决方案的最佳情况是:

  1. SELECT COUNT(*)/COUNT(DISTINCT ref) 对于两个表都很高,约为(10或更多)。
  2. 在两个表中都对 ref 进行了索引(此外,尝试在第一个子查询中为两个表执行 SELECT DISTINCT ref FROM)。
英文:

Possible solution

SELECT refs.ref, sum_header.header_total, sum_items.items_total
FROM ( SELECT ref FROM header
       UNION
       SELECT ref FROM items ) refs
LEFT JOIN ( SELECT ref, SUM(total) header_total
            FROM header
            GROUP BY ref ) sum_header USING (ref)
LEFT JOIN ( SELECT ref, SUM(total) items_total
            FROM items
            GROUP BY ref ) sum_items USING (ref)

The best situation for this solution is:

  1. SELECT COUNT(*)/COUNT(DISTINCT ref) is high, ~ (10 or more), for both tables.
  2. ref is indexed in both tables (additionally try SELECT DISTINCT ref FROM in first subquery for both tables).

答案4

得分: -1

你可以简单地使用以下代码:

选择引用,总计(header.total) 作为 total_header,总计(items.total) 作为 total_items
从 header, items
其中 header.ref = items.ref
按引用降序排序
英文:

You can simply use

Select ref, sum(header.total) as total_header, sum(items.total) as total_items
from header,items where header.ref = items.ref
group by ref
order by ref desc

huangapple
  • 本文由 发表于 2020年1月7日 01:38:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616569.html
匿名

发表评论

匿名网友

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

确定