计算SQL中的净交易头寸

时间:2016-08-16 14:38:30

标签: mysql sql

第一次发帖;在这里找不到任何可以解决我问题的东西。

我有一个约630,000个条目的交易表,这里有示例输入:

dealid deal_counterparty_id deal_instrument_id deal_type deal_amount
 20001                  703               1010 B             3588.81
 20002                  701               1001 S             3412.81
 20003                  701               1004 B             8527.11
 20004                  701               1011 S             2441.77
 20005                  703               1010 B             3633.33
 20006                  702               1011 S             2415.16
 20007                  704               1003 S             1426.14
 20008                  701               1012 B             1858.82
 20009                  703               1009 B             3571.77

我想在每个位置找到每位经销商的实际位置。即交易商为1个交易对手的1个工具进行了数千笔交易,那该交易对手的该工具的净头寸是多少?

我想要3列:deal_counterparty_id,deal_instrument.id,净位置。由于我有20台仪器(1001-1020),只有4个交易对手(701,702,703,704),样本输出如下:

deal_counterparty_id deal_instrument_id net_position
                 701               1001  5833.34
                 701               1002 -3994.21
                 701               1003 30300.00
...
                 702               1001

我的代码(得到0结果):

 select buy.deal_counterparty_id, buy.deal_instrument_id, sum(buy.deal_amount) - sum(sell.deal_amount) as net_position

 from (select deal_id, deal_counterparty_id, deal_instrument_id, deal_type, deal_amount
      from deal where deal_type = 'B') as buy

      join (select deal_id, deal_counterparty_id, deal_instrument_id, deal_type, deal_amount
      from deal where deal_type = 'S') as sell

      on buy.deal_instrument_id = sell.deal_instrument_id

 group by buy.deal_counterparty_id, buy.deal_instrument_id;

谢谢!

2 个答案:

答案 0 :(得分:1)

如果我理解你的要求,这应该有效:

SELECT deal_counterparty_id, deal_instrument_id, 
    SUM( IF( deal_type = 'B', - deal_amount, deal_amount) ) net_position FROM deal
    GROUP BY deal_counterparty_id, deal_instrument_id

那是(编辑为您上次评论时),我认为您正在处理deal_type' B'作为负值和deal_type的' S'作为积极因素,然后将所有内容添加到一起。

答案 1 :(得分:0)

E.g:

DROP TABLE IF EXISTS my_table;

CREATE TABLE my_table
(dealid INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,deal_counterparty_id INT NOT NULL
,deal_instrument_id INT NOT NULL
,deal_type CHAR(1) NOT NULL
,deal_amount DECIMAL(7,2) NOT NULL
);

INSERT INTO my_table VALUES
(20001                  ,703               ,1010 ,'B',             3588.81),
(20002                  ,701               ,1001 ,'S',             3412.81),
(20003                  ,701               ,1004 ,'B',             8527.11),
(20004                  ,701               ,1011 ,'S',             2441.77),
(20005                  ,703               ,1010 ,'B',             3633.33),
(20006                  ,702               ,1011 ,'S',             2415.16),
(20007                  ,704               ,1003 ,'S',             1426.14),
(20008                  ,701               ,1012 ,'B',             1858.82),
(20009                  ,703               ,1009 ,'B',             3571.77);

SELECT deal_counterparty_id
     , deal_instrument_id
     , SUM(CASE WHEN deal_type = 'S' THEN -1*deal_amount ELSE deal_amount END) net_position 
  FROM my_table 
 GROUP 
    BY deal_counterparty_id
     , deal_instrument_id;
+----------------------+--------------------+--------------+
| deal_counterparty_id | deal_instrument_id | net_position |
+----------------------+--------------------+--------------+
|                  701 |               1001 |     -3412.81 |
|                  701 |               1004 |      8527.11 |
|                  701 |               1011 |     -2441.77 |
|                  701 |               1012 |      1858.82 |
|                  702 |               1011 |     -2415.16 |
|                  703 |               1009 |      3571.77 |
|                  703 |               1010 |      7222.14 |
|                  704 |               1003 |     -1426.14 |
+----------------------+--------------------+--------------+