mysql - SUM 连接表的查询如何?

标签 mysql join sum

不知道标题该怎么起。思考标题比写这个问题需要更多时间。
切中要害
假设我有三个表:

//table customers 
|  ID  | Name                  |
++++++++++++++++++++++++++++++++
| 194  | PT Comro Haneut       |
| 195  | PT Kareueut Kameumeut |

//table customer's savings 
|  ID  | IDCustomer | SavingsAmount |
+++++++++++++++++++++++++++++++++++++
|   1  |    194     |    5000000    |
|   2  |    195     |     250000    |
|   3  |    195     |    2500000    |
|   4  |    194     |     125000    |
|   5  |    194     |     175000    |

//table transactions
|  ID  | IDCustomer | Amount        |
+++++++++++++++++++++++++++++++++++++
|  1   |    195     |    1000000    |
|  2   |    195     |     250000    |
|  3   |    194     |    3500000    |
|  4   |    194     |     300000    |

目标
我想将储蓄金额和交易金额相加,并将结果写在一行中,如下所示:

// expected result of the query
| IDCustomer | Savings      | Transactions  | Balance       |
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|   194      |    5300000   |   4800000     |    500000     |
|   195      |    2750000   |   1250000     |   1500000     |

我尝试自己构建查询,但总是失败。我的储蓄和交易金额总计增加了一倍。

有人可以帮忙吗?

最佳答案

查询

select svg.id, Savings, Transactions, (Savings - Transactions) as Balance
from
(
  select c.id as id, sum(s.SavingsAmount) as Savings
  from customers c
  inner join savings s
  on c.id=s.idcustomer
  group by c.id
) svg
inner join
(
  select c.id as id, sum(t.amount) as Transactions
  from customers c
  inner join transactions t
  on c.id=t.idcustomer
  group by c.id
) trans
on svg.id = trans.id
;

设置

create table customers
(
  id integer primary key not null,
  name varchar(23) not null
);

create table savings
(
  id integer primary key not null,
  IDCustomer integer not null,
  SavingsAmount decimal(10, 2) not null,
  foreign key ( IDCustomer ) references customers ( id )
);

create table transactions
(
  id integer primary key not null,
  IDCustomer integer not null,
  amount decimal(10, 2) not null,
  foreign key ( IDCustomer ) references customers ( id )
);

insert into customers
( id, name )
values
( 194  , 'PT Comro Haneut'       ),
( 195  , 'PT Kareueut Kameumeut' )
;

insert into savings 
(  id  , IDCustomer , SavingsAmount )
values
(   1  ,    194     ,    5000000    ),
(   2  ,    195     ,     250000    ),
(   3  ,    195     ,    2500000    ),
(   4  ,    194     ,     125000    ),
(   5  ,    194     ,     175000    )
;

insert into transactions
(  id  , IDCustomer , amount        )
values
(  1   ,    195     ,    1000000    ),
(  2   ,    195     ,     250000    ),
(  3   ,    194     ,    3500000    ),
(  4   ,    194     ,     300000    )
;
<小时/>

输出

+-----+------------+--------------+------------+
| id  | Savings    | Transactions | Balance    |
+-----+------------+--------------+------------+
| 194 | 5300000.00 |   3800000.00 | 1500000.00 |
| 195 | 2750000.00 |   1250000.00 | 1500000.00 |
+-----+------------+--------------+------------+

sqlfiddle

关于mysql - SUM 连接表的查询如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32779550/

相关文章:

python - 复制并合并两个数据框 pandas

sql - 连接的最大计数

for-loop - Odoo - one2many 总和

mysql - MySQL查询来计算销售/总计

python - 在另一个数组中的位置映射 numpy 数组和求和值

java - 如何使用apache camel从mysql表中读取数据并写入另一个表

MySQL LEFT JOIN 不适用于多个表

mysql - 如何使用多个子字符串和子查询来优化此查询

php - 如何一次运行一个查询?

MySQL 多重连接而不合并到同一行