[英]Join two tables to get group by result including columns of both tables
I have two different tables but one tables has primary key of another one. 我有两个不同的表,但是一个表具有另一个的主键。 I am doing group by on one table, but need a particular column in result of another table. 我在一个表上进行分组,但是在另一张表的结果中需要特定的列。
Table1 Transaction { Column transaction_id, Column currency } Table2 Payment { Column payment_id, Column transaction_id, Column user_id, Column pay } 表1交易{列transaction_id,列货币}表2付款{列Payment_id,列transaction_id,列user_id,列支付}
I want to get result in such a way that it is grouped by user_id and currency. 我想以按user_id和货币分组的方式获取结果。 Result has sum of pay for different user_id, currency combination. 结果具有针对不同的user_id,货币组合的支付金额。
For example 例如
Transaction
transaction_id currency
1 Dollar
2 Yun
3 Dollar
Payment
payment_id transaction_id user_id pay
1 1 1 30
2 1 2 20
3 2 1 10
4 3 1 30
Result should have: 结果应具有:
User_id 1
Currency Dollar
Pay 30
user_id 1
Currency Yun
Pay 10
user_id 2
Currency Dollar
Pay 20
This is a very simple Join
这是一个非常简单的Join
Select P.User_Id, T.Currency, Sum(P.Pay)
From Payment P
Join Transaction T On T.Transaction_Id = P.Transaction_Id
Group By P.User_Id, T.Currency
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.