MYSQL:在子查询中使用外部别名

时间:2018-01-21 10:26:26

标签: mysql sql

我试图显示交易付款清单,并在每次付款后显示当前余额。

修改 这是我的架构和示例数据 http://sqlfiddle.com/#!9/77dcc6/1

以下是预期结果的示例

char

这是我最初的尝试,但总付款是根据所有交易付款的总和计算的。我想要的是相对于当前number date total paid balance 1 2018-01-01 1000 1000 0 2 2018-01-02 1250 1000 250 2 2018-01-05 1250 250 0 3 2018-01-03 2500 2000 500 3 2018-01-07 2500 300 200 3 2018-01-08 2500 200 0 5 2018-01-07 2149 2149 0 付款行

的付款总额
p

然后我想我可以在select t.number, DATE(p.date) 'date', ti.total 'total', SUM(p.amount) 'paid', ti.total - paid.total 'balance' from payments p left join transactions t on p.transaction_id = t.id left join ( select inner_ti.transaction_id, sum((inner_ti.price - inner_ti.discount) * inner_ti.quantity) 'total' from transaction_items inner_ti group by inner_ti.transaction_id ) ti on t.id = ti.transaction_id left join ( select inner_p.transaction_id, sum(inner_p.amount) 'total' from payments inner_p group by inner_p.transaction_id ) paid on t.id = paid.transaction_id group by t.number, DATE(p.date), ti.total, paid.total order by t.number, DATE(p.date) ASC 中放置where子句,仅将与paid相关的付款相加,但我收到错误p.date

unknown column p.date

请注意我select t.number, DATE(p.date), ti.total 'total', SUM(p.amount) 'paid', ti.total - paid.total 'balance' from payments p left join transactions t on p.transaction_id = t.id left join ( select inner_ti.transaction_id, sum((inner_ti.price - inner_ti.discount) * inner_ti.quantity) 'total' from transaction_items inner_ti group by inner_ti.transaction_id ) ti on t.id = ti.transaction_id left join ( select inner_p.transaction_id, sum(inner_p.amount) 'total' from payments inner_p where inner_p.date <= p.date -- error unknown column p.date group by inner_p.transaction_id ) paid on t.id = paid.transaction_id group by t.number, DATE(p.date), ti.total, paid.total order by DATE(p.date) ASC 分组,因为我们担心当天付款。

有人可以告诉我为什么我会收到这个错误吗?是否有任何解决方法可以达到预期效果?

谢谢!

1 个答案:

答案 0 :(得分:2)

也许你应该重新开始

drop table if exists p,t,ti;

create table t(id int,number int);
create table ti(id int,tid int, price int);
create table p(id int,tid int,dt date, paid int);

insert into t values (1,1355),(2,1359),(3,1361);
insert into ti values (1,1,400),(2,1,200),(3,1,299),(4,2,4045),(5,3,1500),(6,3,40),(7,3,8);
insert into p values (1,1,'2018-01-01',200),(2,1,'2018-01-01',250),(3,1,'2018-02-01',449),
                            (4,2,'2018-01-01',1515),(5,2,'2018-02-01',35),
                            (6,3,'2018-01-06',1548);

select t.id,number,p.dt paiddate,(select sum(price) from ti where ti.tid = t.id) due,
        sum(p.paid) paid,
        ifnull((Select sum(p1.paid) from p p1 where p1.dt < p.dt and p1.tid = p.tid),0) +   sum(p.paid) aggsumpaid,
        (select sum(price) from ti where ti.tid = t.id) - 
        (ifnull((Select sum(p1.paid) from p p1 where p1.dt < p.dt and p1.tid = p.tid),0) +  sum(p.paid)) runningbal
from t 
left join p on p.tid = t.id
group by t.id,number,p.dt

结果

+------+--------+------------+------+------+------------+------------+
| id   | number | paiddate   | due  | paid | aggsumpaid | runningbal |
+------+--------+------------+------+------+------------+------------+
|    1 |   1355 | 2018-01-01 |  899 |  450 |        450 |        449 |
|    1 |   1355 | 2018-02-01 |  899 |  449 |        899 |          0 |
|    2 |   1359 | 2018-01-01 | 4045 | 1515 |       1515 |       2530 |
|    2 |   1359 | 2018-02-01 | 4045 |   35 |       1550 |       2495 |
|    3 |   1361 | 2018-01-06 | 1548 | 1548 |       1548 |          0 |
+------+--------+------------+------+------+------------+------------+
5 rows in set (0.02 sec)