首次發佈;在這裏找不到解決我的問題的任何東西。計算SQL中的淨貿易頭寸
我有大約63萬項,這裏樣品輸入一個交易表:
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至20年)只有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;
謝謝!
樣本輸入輸出+?不像每個開發者都是財務人 – Steve
樣本輸入是上面的表格圖像,樣本輸出將是: deal_counterparty_id || deal_instrument_id ||淨頭寸 701 || 1001 || -3495.40 701 || 1002 || 7825.01 ... 701 || 1020 || 1938.23 702 || 1001 || 4726.44 –
而不是'JOIN's,我會從'IF(deal_type ='B',deal_amount, - deal_amount)net_amount'開始。 –