缺少Group by和Sum的数据

时间:2017-09-14 17:01:03

标签: mysql group-by

我试图理解一个特定案例中的group by子句:我加入了两张表,每张发票应该有相同的金额,所以我试图查询确认没有差异。这是表格,查询和结果:

Table A:
Order_Number | Order_Number_Line | Amount
1            | 1                 | 80
1            | 2                 | 20
1            | 3                 | 30
1            | 4                 | 70

Table B

Order_Number | Order_Number_Line | Invoice_ID | Invoice_Line_Number | Amount
1            | 1                 | 1234       | 1                   | 20
1            | 2                 | 1234       | 2                   | 5
1            | 3                 | 1234       | 3                   | 10
1            | 4                 | 1234       | 4                   | 25
1            | 1                 | 1234       | 1                   | 35
1            | 2                 | 1234       | 2                   | 7
1            | 3                 | 1234       | 3                   | 15
1            | 4                 | 1234       | 4                   | 10
1            | 1                 | 1234       | 1                   | 25
1            | 2                 | 1234       | 2                   | 8
1            | 3                 | 1234       | 3                   | 5
1            | 4                 | 1234       | 4                   | 35

Select A.Order_Number, A.Amount, B.Amount
From Table_A as A
Left Join (Select Order_Number, Amount From Table_B) as B
On A.Order_Number = B.Order_Number

Order_Number  |  Amount_Table_A  |  Amount_Table_B 
01            |  80              |   20            
01            |  80              |   5            
01            |  80              |   10            
01            |  80              |   25            
01            |  80              |   35            
01            |  80              |   7            
01            |  80              |   15           
01            |  80              |   10         
01            |  80              |   25            
01            |  80              |   8          
01            |  80              |   5           
01            |  80              |   35
01            |  20              |   20            
01            |  20              |   5            
01            |  20              |   10            
01            |  20              |   25            
01            |  20              |   35            
01            |  20              |   7            
01            |  20              |   15           
01            |  20              |   10         
01            |  20              |   25            
01            |  20              |   8          
01            |  20              |   5           
01            |  20              |   35
01            |  30              |   20            
01            |  30              |   5            
01            |  30              |   10            
01            |  30              |   25            
01            |  30              |   35            
01            |  30              |   7            
01            |  30              |   15           
01            |  30              |   10         
01            |  30              |   25            
01            |  30              |   8          
01            |  30              |   5           
01            |  30              |   35
01            |  70              |   20            
01            |  70              |   5            
01            |  70              |   10            
01            |  70              |   25            
01            |  70              |   35            
01            |  70              |   7            
01            |  70              |   15           
01            |  70              |   10         
01            |  70              |   25            
01            |  70              |   8          
01            |  70              |   5           
01            |  70              |   35            

我尝试使用group by,但只获得了每张表的第一笔金额记录:

Select A.Order_Number, A.Amount, B.Amount
From Table_A as A
Left Join (Select Order_Number, Amount From Table_B) as B
On A.Order_Number = B.Order_Number
Group By A.Order_Number

 Invoice_ID  |  Amount_Table_A  |  Amount_Table_B  |
 01          |  80              |  20              |

我尝试将sum()子句添加到select语句中的每个amount属性,并得到每个重复记录的总和:

Select A.Order_Number, sum(A.Amount), sum(B.Amount)
From Table_A as A
Left Join (Select Order_Number, Amount From Table_B) as B
On A.Order_Number = B.Order_Number
Group By A.Order_Number

 Invoice_ID  |  Amount_Table_A  |  Amount_Table_B  |
 01          |  2400            |  800             |

如何修改此查询以显示实际值?预期价值是:

 Invoice_ID  |  Amount_Table_A  |  Amount_Table_B  |
 01          |  200             |  200             |

请记住,这是一个过度简化的数据集。真实表有超过一百万条记录,我需要确保Invoice_ID在两个表中的总和相同。

非常感谢!!!

2 个答案:

答案 0 :(得分:1)

你很亲密,试试这个。这并不是100%清楚你想要做什么,但我希望这会有所帮助。您的加入似乎没有足够的选择性。

Select A.Order_Number, SUM(A.Amount), SUM(B.Amount)
From Table_A as A
Left Join Table_B as B
On A.Order_Number = B.Order_Number AND A.Order_Line_Number = B.Order_Line_Number
Group By A.Order_Number

答案 1 :(得分:1)

试试这个并告诉我这是否适合您。

SELECT 
    A.Order_Number, A.Amount AS A_Amount, B.Amount AS B_Amount
FROM
    (SELECT 
        Order_Number, SUM(Amount) AS Amount
    FROM
        A
    GROUP BY Order_Number) A
        JOIN
    (SELECT 
        Order_Number, SUM(Amount) AS Amount
    FROM
        B
    GROUP BY Order_Number) B ON A.Order_Number = B.Order_Number