dongtong848825
dongtong848825
2018-05-07 20:56

mysql运行总计付款分组列,共享代码

已采纳

I am trying to create a running total of payments for each portfolio of stocks/shares, where there are multiple stock purchase transactions for a given stock and portfolio, I need to group the stock into a single line of stock by grouping the quantity and payment for that line.

Table Schema and data below and query and results of query follow

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;

query shown below followed by results

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;

results of query, followed by desired results

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

Desired results, are as above except BLND being the only share having more than one transaction is grouped/summated as follows for quantity and payment (2 lines is summated to a single line)

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

etc ...

Many Thanks in advance for any suggestions on how to achieve this result.

Colin

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dqnek0079 dqnek0079 3年前

    You can extend your existing query with another sub-query as follows.

    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;
    

    and it will provide you with the appropriate output as you requested.

    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
    

    Good Luck!

    点赞 评论 复制链接分享