'SQL query to verify account trade fee logic given data from another table
I would like to verify a table 'account trade fee' columns field are correctly recorded based on another table 'exchange trade'.
Since I am really new to coding, would like to seek some help from yours.
Give background is now there is table account trade fee, as you see below there are column name 'fee_currency','maker_fee' and 'taker_fee'.
account_trade_fee
"account_uuid","fee_currency","maker_fee","taker_fee"
c3f94bdf-8854-49d2-833d-0690eb821aa1,BTC,0.0002,0.0002
c3f94bdf-8854-49d2-833d-0690eb821aa1,USDT,0.02,0.02
c3f94bdf-8854-49d2-833d-0690eb821aa2,CRO,0.0002,0.0002
c3f94bdf-8854-49d2-833d-0690eb821aa2,USDT,0.02,0.02
exchange_trade
"account_uuid","buy_fee_scale","buy_fee_value","buy_fee_currency","sell_fee_scale","sell_fee_value","sell_fee_currency","taker_side"
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,BUY
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,BUY
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,BUY
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,BUY
c3f94bdf-8854-49d2-833d-0690eb821aa1,10,400000,BTC,8,400000,USDT,BUY
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,SELL
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,BUY
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,BUY
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,BUY
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,BUY
c3f94bdf-8854-49d2-833d-0690eb821aa2,10,400000,CRO,8,400000,USDT,BUY
There is equation of calculating the maker fee and taker fee from exchange trade table. If from below column 'taker_side':
if taker_side = 'BUY' refers to a maker fee, BTC would be the buy_fee_currency.
which the formula would be buy_fee_value/pow(10,buy_fee_scale) = 0.00004.
Since there is total five trade from BTC_USDT pair are BUY side.
So the final maker fee would be 0.00004 * 5 = 0.0002 for BTC currency, which shows the same result from account trade fee above.
As BTC would be the buy_fee_currency, then USDT would be the sell_fee_curreny so then it will calculated as taker_fee and formula is like
sell_fee_value/pow(10,sell_fee_scale) = 0.004 . Since there are total five trade are "SELL" side, times 5 would equal to 0.004 * 5 = 0.02
vice versa to the case if taker_side is "SELL" BTC is sell_fee_currency. USDT is buy_fee_currency.
I could only manage to write some sql query like below , but it couldn't generate the expected sum as I don't know how to control the calculation with regard to maker side or buyer side.In order to verify if the logic apply correctly, I would like to write a query to check if the sum of each currency is correctly display in maker_fee and taker_fee. Could any one kindly advise how to do so?
select account_uuid , taker_side
case
when taker_side = 'BUY'
then (
sum((buy_fee_value/power(10,buy_fee_scale)) as maker_fee,
select buy_fee_currency as fee_currecny )
end
case
when taker_side = 'SELL'
then (
sum((sell_fee_value/power(10,sell_fee_scale)) as taker_fee,
select sell_fee_currency as fee_currecny )
end
from exchange_trade et
group by account_uuid ,taker_side
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
