简体   繁体   English

两个表的SQL总和

[英]SQL sum of two tables

I have these two tables: 我有这两个表:

I need to join the payment table with the discount table. 我需要将付款表与折扣表连接起来。 The expected output seems not possible since the discounts table doesn't have a payment date. 由于折扣表没有付款日期,因此无法实现预期的输出。 I can only get the net_amount 我只能得到net_amount

payment table:
id    |  net_amount  |  payment_dt  |  person_id
1001  |  2765.36     |  2016-05-28  |  372
1002  |  2474.76     |  2016-05-29  |  372
1003  |  22694.25    |  2016-05-29  |  384
1004  |  1911.92     |  2016-05-29  |  384

discounts table:
id |  person_id  |  gross_amount | sc_discount | other_discount_amount | other_discount_type
1  |  372        |  3566.7       | 713.34      | 88.00                 | MISC 
2  |  372        |  3202.2       | 640.44      | 87.00                 | PAT
3  |  384        |  3566.7       | 713.34      | 285.34                | MISC
4  |  384        |  27953.10     | 5590.62     | 2236.25               | PAT
5  |  384        |  2655.45      | 531.09      | 212.44                | MISC

*1 - payment_dt is 2016-05-28

expected output: (where payment_dt=2016-05-29)
total_gross_amount | total_sc_discount | total_misc_discount | total_pat_discount | total_net_amount
37,377.45          | 7475.49           | 497.78              | 2,323.25           | 27,080.93

As I see in both tables common column is person_id, you can try to join with it. 正如我在两个表中看到的,公共列是person_id,您可以尝试将其加入。 And for more information you can have in mind natural joins, read about it on The Internet ;) 有关更多信息,您可以考虑自然连接,请在Internet上阅读有关信息;)

"A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables. A NATURAL JOIN can be an INNER join, a LEFT OUTER join, or a RIGHT OUTER join. The default is INNER join. " “ NATURAL JOIN是一个JOIN操作,它基于要连接的两个表中的公共列为您创建一个隐式连接子句。公共列是两个表中具有相同名称的列。NATURAL JOIN可以是INNER连接, “ LEFT OUTER连接或RIGHT OUTER连接。默认值为INNER连接。”

By assuming : net_amount = gross_amount - sc_discount - other_discount_amount 通过假设:net_amount = gross_amount-sc_discount-other_discount_amount

You do not need to go to payments table for total_net_amount in the expected output 您无需转到预期输出中的total_net_amount的付款表

You can write it like this : 您可以这样写:

Select sum(gross_amount)  as total_gross_amount,
sum(sc_discount) as total_sc_discount,
sum(gross_amount - sc_discount - other_discount_amount) as total_net_amount
sum(CASE other_discount_type when 'MISC' THEN other_discount_amount WHEN 'PAT' THEN 0) as total_misc_discount,
sum(CASE other_discount_type when 'MISC' THEN 0 WHEN 'PAT' THEN other_discount_amount) as total_pat_discount    
from discounts

And in case the above assumption is not true, As the aggregation is complete, only 1 row will come in output, you can get all columns except total_net_amount as in above query, get sum(net_amount) from payments table, and join them on true as only 1,1 row. 如果上述假设不成立,则在汇总完成后,只有1行会输出,您可以像上述查询一样获得除total_net_amount之外的所有列,从付款表中获取sum(net_amount)并将它们联接为true仅1,1行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM