gpt4 book ai didi

php - mysql运行按列分组的付款总额,共享代码

转载 作者:行者123 更新时间:2023-11-29 10:15:07 24 4
gpt4 key购买 nike

我正在尝试为每个股票/股票投资组合创建连续的付款总额,其中给定股票和投资组合有多个股票购买交易,我需要通过将股票分组为单行股票该行的数量和付款。

下面的表架构和数据以及查询和查询结果

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

等等...

提前非常感谢您提供有关如何实现此结果的任何建议。

科林

最佳答案

您可以使用另一个子查询来扩展现有查询,如下所示。

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 ( select
code,
portfolio,
pdate,
dividend,
sum(quantity) as quantity
from test
group by portfolio, pdate,code, dividend
order by portfolio, pdate,code, dividend ) as SubQueryAlias1
cross join
( select @row_num := 0, @balance := 0, @prev_pfl := '' ) as InitVarsAlias1
order by portfolio, pdate,code) as SubQueryAlias2
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 3286 248 248
2 3 AV. AJB_SIPP_CO 2018-05-17 15.88 2135 340 588
3 17 AV. SFT_DEA_CO 2018-05-17 15.88 2318 369 369
4 4 DLG AJB_SIPP_CO 2018-05-18 9.70 2732 266 854
5 18 DLG SFT_DEA_CO 2018-05-18 9.70 2789 271 640
6 5 SLA AJB_SIPP_CO 2018-05-23 13.35 2820 377 1231
7 19 SLA SFT_DEA_CO 2018-05-23 13.35 3247 434 1074
8 6 PHP AJB_SIPP_CO 2018-05-27 1.31 6947 92 1323
9 7 LLOY AJB_SIPP_CO 2018-05-29 2.05 15519 319 1642
10 14 LLOY SFT_DEA_CL 2018-05-29 2.05 40011 821 821
11 23 LLOY SFT_ISA_CO 2018-05-29 2.05 7973 164 164
12 1 FCPT AJB_SIPP_CL 2018-05-31 0.50 223 2 2
13 8 FCPT AJB_SIPP_CO 2018-05-31 0.50 5837 30 1672
14 9 RLSEB AJB_SIPP_CO 2018-05-31 1.80 5021 91 1763
15 21 FCPT SFT_ISA_CL 2018-05-31 0.50 3609 19 19
16 22 RLSEB SFT_ISA_CL 2018-05-31 1.80 2100 38 57
17 24 FCPT SFT_ISA_CO 2018-05-31 0.50 5136 26 190
18 25 RLSEB SFT_ISA_CO 2018-05-31 1.80 2100 38 228
19 10 LGEN AJB_SIPP_CO 2018-06-08 10.35 3923 407 2170
20 15 LGEN SFT_DEA_CL 2018-06-08 10.35 10652 1103 1924
21 11 BP. AJB_SIPP_CO 2018-06-23 7.67 2130 164 2334
22 12 RDSB AJB_SIPP_CO 2018-06-23 35.02 436 153 2487
23 16 RDSB SFT_DEA_CL 2018-06-23 35.02 1292 453 2377
24 20 BP. SFT_DEA_CO 2018-06-23 7.67 446 35 1109
25 13 CNA AJB_SIPP_CO 2018-06-29 8.40 7512 632 311

祝你好运!

关于php - mysql运行按列分组的付款总额,共享代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50222313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com