前回作ったテーブルとデータで集計するならどんなSQL文になるか。仮で考えてみた。
ブツ
前回
集計項目
- 支払総額
- 手数料
- 手数料比
- 受取総額
- 残高
- 取引件数
- 支払回数
- 受取回数
- 支払アドレス数
- 受取アドレス数
- 両思アドレス数
支払総額
select ((sum(value)) * POW(0.1, 8)) from transactions where is_send = 1;
const txs = await dexie.transactions.toArray()
txs.filter(tx=>tx.isSend).map(tx=>tx.value).reduce((sum,v)=>sum+v)*(0.1**8)
支払時はvalueに手数料feeが加算されている。
手数料
select (sum(fee) * POW(0.1, 8)) from transactions where is_send = 1;
txs.filter(tx=>tx.isSend).map(tx=>tx.fee).reduce((sum,v)=>sum+v)*(0.1**8)
手数料比(%)
(手数料 / 支払総額) * 100
select ((sum(fee) * POW(0.1, 8)) / (sum(value) * POW(0.1, 8))) * 100 from transactions where is_send = 1;
const sends = txs.filter(tx=>tx.isSend)
const send = sends.map(tx=>tx.value).reduce((sum,v)=>sum+v)
const fee = sends.map(tx=>tx.fee).reduce((sum,v)=>sum+v)
(fee / (send + fee)) * (0.1**8) * 100
受取総額
select (sum(value) * POW(0.1, 8)) from transactions where is_send = 0 and 0 < confirmations;
または以下でも可。
select (sum(value) * POW(0.1, 8)) from receive_partners;
txs.filter(tx=>!tx.isSend).map(tx=>tx.value).reduce((sum,v)=>sum+v)*(0.1**8)
残高
受取総額 - 支払総額
select (receive - send)*POW(0.1,8) balance from (select sum(value) receive from transactions where is_send = 0 and 0 < confirmations) inner join (select sum(value) send from transactions where is_send = 1);
const receive = txs.filter(tx=>!tx.isSend).map(tx=>tx.value).reduce((sum,v)=>sum+v)*(0.1**8)
const send = txs.filter(tx=>tx.isSend).map(tx=>tx.value+tx.fee).reduce((sum,v)=>sum+v)
receive - send
取引件数
select count(*) from transactions;
支払回数
select count(*) from transactions where is_send = 1;
受取回数
select count(*) from transactions where is_send = 0 and 0 < confirmations;
支払アドレス数
select count(*) from send_partners;
または以下。
select count(*) from (select distinct addresses from transactions where is_send = 1);
const send = partners.length
const send = txs.filter(tx=>tx.isSend).length
両思アドレス数
select count(*) from (
select address from send_partners
INTERSECT
select address from receive_partners);
両思アドレスを一覧するなら以下。
select address from send_partners
INTERSECT
select address from receive_partners;