根据其他值更新单元格中的值

时间:2019-02-26 21:56:52

标签: sql sql-server stored-procedures

我有一个名为temptable的表,我必须根据'trantype'和'tradedate'更新每个'cusip'的'quantity'。逻辑为sum(BUY)== sum(SELL),当SELL之后再次出现BUY时,重复相同的逻辑。因此,我想到了一个随机生成所有BUY值并根据这些值操纵SELL值的想法。

这是表脚本和一些示例数据:

newDBName = "${env.accountName}"

这就是我想要获得的:

示例1:

newDBName = env.accountName

扩展结果:

CREATE TABLE temptable(
TRANTYPE  VARCHAR(4) NOT NULL 
,CUSIP     VARCHAR(9) NOT NULL
,TRADEDATE DATE  NOT NULL
,QUANTITY  INTEGER 
);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ003$03','2018-08-01',3517150);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ004$04','2018-08-01',2575357);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ102$02','2018-08-01',6523548);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ006$06','2018-08-02',9429433);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ006$06','2018-08-02',9240844);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ006$06','2018-08-02',9056027);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ007$07','2018-08-02',4266903);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ104$04','2018-08-02',6914360);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ004$04','2018-08-03',2523849);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('SELL','XYZ007$07','2018-08-08',NULL);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('BUY','XYZ007$07','2018-08-15',4097932);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('SELL','XYZ006$06','2018-09-04',NULL);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('SELL','XYZ102$02','2018-10-05',NULL);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('SELL','XYZ003$03','2018-10-18',NULL);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('SELL','XYZ003$03','2018-10-24',NULL);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('SELL','XYZ003$03','2018-10-26',NULL);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('SELL','XYZ003$03','2018-10-29',NULL);
INSERT INTO temptable(TRANTYPE,CUSIP,TRADEDATE,QUANTITY) VALUES ('SELL','XYZ007$07','2018-08-16',NULL);

示例2:

SELECT * FROM [dbo].temptable WHERE CUSIP ='SCD007$07';

扩展结果:

-- +----------+-----------+------------+----------+
-- | TRANTYPE |   CUSIP   | TRADEDATE  | QUANTITY |
-- +----------+-----------+------------+----------+
-- | BUY      | XYZ007$07 | 2018-08-02 |  4266903 |
-- | SELL     | XYZ007$07 | 2018-08-08 |  4266903 |
-- | BUY      | XYZ007$07 | 2018-08-15 |  4097932 |
-- | SELL     | XYZ007$07 | 2018-08-16 |  4097932 |
-- +----------+-----------+------------+----------+

这是我尝试过的:

SELECT * FROM temptable WHERE CUSIP = 'SCD003$03';`

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您要更新出售记录(quantity的{​​{1}},并将其设置为之前 buy < / em>(trantype = 'SELL')的同一客户(quantity),其中记录按trantype = 'BUY'排序。当两次购买之间有多个销售记录时,您希望将先前购买中的cusip平均分配到销售记录中。

这是一个产生预期输出的SQLServer 2017查询。基本上,它会自动tradedate与相应的销售记录一起购买记录;使用条件为quantity的相关子查询来查找相关的销售记录(第一个记录)。然后,窗口函数JOIN用于根据连续销售记录的数量划分数量。该查询还会考虑现有的销售数量(如果有)。

NOT EXISTS

现在,如果您要更新源表,可以将查询转到CTE。可以简化查询,因为它只需要影响空的销售记录。

COUNT(*) OVER(...)

Demo on DB Fiddle

TRANTYPE | CUSIP     | TRADEDATE           | QUANTITY
:------- | :-------- | :------------------ | -------:
BUY      | SCD003$03 | 01/08/2018 00:00:00 |  3517150
SELL     | SCD003$03 | 18/10/2018 00:00:00 |   879287
SELL     | SCD003$03 | 24/10/2018 00:00:00 |   879287
SELL     | SCD003$03 | 26/10/2018 00:00:00 |   879287
SELL     | SCD003$03 | 29/10/2018 00:00:00 |   879287
BUY      | SCD004$04 | 01/08/2018 00:00:00 |  2575357
BUY      | SCD004$04 | 03/08/2018 00:00:00 |  2523849
BUY      | SCD006$06 | 02/08/2018 00:00:00 |  9429433
BUY      | SCD006$06 | 03/08/2018 00:00:00 |  9240844
BUY      | SCD006$06 | 04/08/2018 00:00:00 |  9056027
SELL     | SCD006$06 | 04/09/2018 00:00:00 |  9056027
BUY      | SCD007$07 | 02/08/2018 00:00:00 |  4266903
SELL     | SCD007$07 | 08/08/2018 00:00:00 |  4266903
BUY      | SCD007$07 | 15/08/2018 00:00:00 |  4097932
SELL     | SCD007$07 | 16/08/2018 00:00:00 |  4097932
BUY      | SCD102$02 | 01/08/2018 00:00:00 |  6523548
SELL     | SCD102$02 | 05/10/2018 00:00:00 |  6523548
BUY      | SCD104$04 | 02/08/2018 00:00:00 |  6914360