我正在尝试为每个股票/股票组合创建一个连续的付款总额,如果给定的股票和投资组合有多个股票购买交易,我需要通过对该行的数量和付款进行分组,将股票分组为一行股票。
下面是表模式和数据,然后是查询和查询结果
CREATE TABLE `test` (
`code` varchar(10) DEFAULT NULL,
`portfolio` varchar(30) DEFAULT NULL,
`pdate` date DEFAULT NULL,
`dividend` decimal(12,2) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
`payment` double(12,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
LOCK TABLES `test` WRITE;
/*!40000 ALTER TABLE `test` DISABLE KEYS */;
INSERT INTO `test` VALUES
('BLND','AJB_SIPP_CO','2018-05-05',7.52,1643,124.00),
('BLND','AJB_SIPP_CO','2018-05-05',7.52,1643,124.00),
('AV.','AJB_SIPP_CO','2018-05-17',15.88,2135,340.00),
('AV.','SFT_DEA_CO','2018-05-17',15.88,2318,369.00),
('DLG','AJB_SIPP_CO','2018-05-18',9.70,2732,266.00),
('DLG','SFT_DEA_CO','2018-05-18',9.70,2789,271.00),
('SLA','AJB_SIPP_CO','2018-05-23',13.35,2820,377.00),
('SLA','SFT_DEA_CO','2018-05-23',13.35,3247,434.00),
('PHP','AJB_SIPP_CO','2018-05-27',1.31,6947,92.00),
('LLOY','AJB_SIPP_CO','2018-05-29',2.05,15519,319.00),
('LLOY','SFT_DEA_CL','2018-05-29',2.05,40011,821.00),
('LLOY','SFT_ISA_CO','2018-05-29',2.05,7973,164.00),
('FCPT','AJB_SIPP_CL','2018-05-31',0.50,223,2.00),
('FCPT','AJB_SIPP_CO','2018-05-31',0.50,5837,30.00),
('RLSEB','AJB_SIPP_CO','2018-05-31',1.80,5021,91.00),
('FCPT','SFT_ISA_CL','2018-05-31',0.50,3609,19.00),
('RLSEB','SFT_ISA_CL','2018-05-31',1.80,2100,38.00),
('FCPT','SFT_ISA_CO','2018-05-31',0.50,5136,26.00),
('RLSEB','SFT_ISA_CO','2018-05-31',1.80,2100,38.00),
('LGEN','AJB_SIPP_CO','2018-06-08',10.35,3923,407.00),
('LGEN','SFT_DEA_CL','2018-06-08',10.35,10652,1103.00),
('BP.','AJB_SIPP_CO','2018-06-23',7.67,2130,164.00),
('RDSB','AJB_SIPP_CO','2018-06-23',35.02,436,153.00),
('RDSB','SFT_DEA_CL','2018-06-23',35.02,1292,453.00),
('BP.','SFT_DEA_CO','2018-06-23',7.67,446,35.00),
('CNA','AJB_SIPP_CO','2018-06-29',8.40,7512,632.00);
/*!40000 ALTER TABLE `test` ENABLE KEYS */;
UNLOCK TABLES;
查询结果如下所示
select
@row_n := @row_n + 1 as row_n,
row_num,
code,
portfolio,
pdate,
dividend,
quantity,
payment,
balance
from ( select
@row_num := @row_num + 1 as row_num,
code,
portfolio,
pdate,
dividend,
quantity,
ceiling(dividend*quantity/100) as payment,
@balance := ceiling(dividend*quantity/100) + if (@prev_pfl = portfolio,@balance,0) as balance,
@prev_pfl := portfolio as prev_portfolio
from test
cross join
( select @row_num := 0, @balance := 0, @prev_pfl := '' ) as InitVarsAlias
order by portfolio, pdate,code) as SubQueryAlias
cross join
(select @row_n := 0 ) as InitVarsAlias2
order by pdate,portfolio,code,row_num;
查询结果,后跟所需结果
row_n row_num code portfolio pdate dividend quantity payment balance
1 2 BLND AJB_SIPP_CO 2018-05-05 7.52 1643 124 124
2 3 BLND AJB_SIPP_CO 2018-05-05 7.52 1643 124 248
3 4 AV. AJB_SIPP_CO 2018-05-17 15.88 2135 340 588
4 18 AV. SFT_DEA_CO 2018-05-17 15.88 2318 369 369
5 5 DLG AJB_SIPP_CO 2018-05-18 9.70 2732 266 854
6 19 DLG SFT_DEA_CO 2018-05-18 9.70 2789 271 640
7 6 SLA AJB_SIPP_CO 2018-05-23 13.35 2820 377 1231
8 20 SLA SFT_DEA_CO 2018-05-23 13.35 3247 434 1074
9 7 PHP AJB_SIPP_CO 2018-05-27 1.31 6947 92 1323
10 8 LLOY AJB_SIPP_CO 2018-05-29 2.05 15519 319 1642
11 15 LLOY SFT_DEA_CL 2018-05-29 2.05 40011 821 821
12 24 LLOY SFT_ISA_CO 2018-05-29 2.05 7973 164 164
13 1 FCPT AJB_SIPP_CL 2018-05-31 0.50 223 2 2
14 9 FCPT AJB_SIPP_CO 2018-05-31 0.50 5837 30 1672
15 10 RLSEB AJB_SIPP_CO 2018-05-31 1.80 5021 91 1763
16 22 FCPT SFT_ISA_CL 2018-05-31 0.50 3609 19 19
17 23 RLSEB SFT_ISA_CL 2018-05-31 1.80 2100 38 57
18 25 FCPT SFT_ISA_CO 2018-05-31 0.50 5136 26 190
19 26 RLSEB SFT_ISA_CO 2018-05-31 1.80 2100 38 228
20 11 LGEN AJB_SIPP_CO 2018-06-08 10.35 3923 407 2170
21 16 LGEN SFT_DEA_CL 2018-06-08 10.35 10652 1103 1924
22 12 BP. AJB_SIPP_CO 2018-06-23 7.67 2130 164 2334
23 13 RDSB AJB_SIPP_CO 2018-06-23 35.02 436 153 2487
24 17 RDSB SFT_DEA_CL 2018-06-23 35.02 1292 453 2377
25 21 BP. SFT_DEA_CO 2018-06-23 7.67 446 35 1109
26 14 CNA AJB_SIPP_CO 2018-06-29 8.40 7512 632 3119
预期结果如上所述,除了blnd是唯一有一笔以上交易的股份,其数量和付款分组/汇总如下(2行汇总为一行)
row_n row_num code portfolio pdate dividend quantity payment balance
1 2 BLND AJB_SIPP_CO 2018-05-05 7.52 3286 248 248
2 4 AV. AJB_SIPP_CO 2018-05-17 15.88 2135 340 588
3 18 AV. SFT_DEA_CO 2018-05-17 15.88 2318 369 369
4 5 DLG AJB_SIPP_CO 2018-05-18 9.70 2732 266 854
5 19 DLG SFT_DEA_CO 2018-05-18 9.70 2789 271 640
6 6 SLA AJB_SIPP_CO 2018-05-23 13.35 2820 377 1231
7 20 SLA SFT_DEA_CO 2018-05-23 13.35 3247 434 1074
等。。。
非常感谢您对如何实现这一结果提出的任何建议。
科林
1条答案
按热度按时间2skhul331#
可以使用另一个子查询扩展现有查询,如下所示。
它将根据您的要求提供适当的输出。
祝你好运!