计算累计收益,fund_account, init_date, total_income
代码语言:javascript
复制
drop table if exists data_stock; create table data_stock (account varchar(10),amount int,init_date varchar(20));
INSERT INTO
data_stock
VALUES ('2002', 210, '20170101');
INSERT INTOdata_stock
VALUES ('2001', 70, '20170101');
INSERT INTOdata_stock
VALUES ('2001', 200, '20170101');
INSERT INTOdata_stock
VALUES ('2001', 30, '20170103');
INSERT INTOdata_stock
VALUES ('2002', 10, '20170102');SELECT * from data_stock;
SELECT * from
data_stock a LEFT JOIN data_stock b on a.account=b.account where a.init_date>=b.init_date
group BY a.account,a.init_date;
-- 关联条件 (可以算累计收益)
SELECT a.account,a.init_date,sum(b.amount) as total from
data_stock a left join data_stock b on a.account = b.account where a.init_date>=b.init_date
group BY a.account,a.init_date;