计算累计收益

计算累计收益,fund_account, init_date, total_income 

 

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 INTO `data_stock` VALUES ('2001', 70, '20170101');
INSERT INTO `data_stock` VALUES ('2001', 200, '20170101');
INSERT INTO `data_stock` VALUES ('2001', 30, '20170103');
INSERT INTO `data_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;

 

posted @ 2018-04-16 18:54  QualityAssurance21  阅读(826)  评论(0编辑  收藏  举报