Skip to content

Commit 4612d11

Browse files
cexplorererikd
authored andcommitted
add "Query all delegators and some basic info"
1 parent 8092aa6 commit 4612d11

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

doc/interesting-queries.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,46 @@ select distinct on(block.hash) block.hash as block_hash , epoch_no, tx_count, po
569569
```
570570
---
571571

572+
### Query all delegators and some basic info
573+
```sql
574+
SELECT sa.view AS address,
575+
nh.view AS delegated_now_poolid,
576+
d.active_epoch_no delegated_now_epoch,
577+
oh.view AS delegated_before_poolid,
578+
od.active_epoch_no AS delegated_before_epoch,
579+
d.addr_id,
580+
br."time" AS stake_key_registered
581+
FROM ( SELECT max(delegation.id) AS id
582+
FROM delegation
583+
GROUP BY delegation.addr_id) a
584+
LEFT JOIN delegation d ON d.id = a.id
585+
LEFT JOIN tx txn ON txn.id = d.tx_id
586+
LEFT JOIN block bn ON bn.id = txn.block_id
587+
LEFT JOIN stake_address sa ON sa.id = d.addr_id
588+
LEFT JOIN stake_deregistration de ON de.addr_id = d.addr_id AND de.id = (( SELECT max(stake_deregistration.id) AS max
589+
FROM stake_deregistration
590+
WHERE stake_deregistration.addr_id = d.addr_id))
591+
LEFT JOIN stake_registration re ON re.addr_id = d.addr_id AND re.id = (( SELECT max(stake_registration.id) AS max
592+
FROM stake_registration
593+
WHERE stake_registration.addr_id = d.addr_id))
594+
LEFT JOIN delegation od ON od.addr_id = d.addr_id AND od.id = (( SELECT max(delegation.id) AS max
595+
FROM delegation
596+
WHERE delegation.addr_id = d.addr_id AND NOT delegation.pool_hash_id = d.pool_hash_id))
597+
LEFT JOIN tx txo ON txo.id = od.tx_id
598+
LEFT JOIN block bo ON bo.id = txo.block_id
599+
LEFT JOIN pool_hash nh ON nh.id = d.pool_hash_id
600+
LEFT JOIN pool_hash oh ON oh.id = od.pool_hash_id
601+
LEFT JOIN tx txr ON txr.id = re.tx_id
602+
LEFT JOIN block br ON br.id = txr.block_id
603+
WHERE (de.tx_id < re.tx_id OR de.* IS NULL);
604+
605+
606+
address | delegated_now_poolid | delegated_now_epoch | delegated_before_poolid | delegated_before_epoch | addr_id | stake_key_registered
607+
-------------------------------------------------------------+----------------------------------------------------------+---------------------+----------------------------------------------------------+------------------------+---------+----------------------
608+
stake1u9ylzsgxaa6xctf4juup682ar3juj85n8tx3hthnljg47zctvm3rc | pool1pu5jlj4q9w9jlxeu370a3c9myx47md5j5m2str0naunn2q3lkdy | 210 | | | 1 | 2020-07-29 22:41:31
609+
stake1u8mt5gqclkq0swmvzx9lvq4jgwsnx9yh030yrxwqwllu0mq2m0l4n | pool1qqqqzyqf8mlm70883zht60n4q6uqxg4a8x266sewv8ad2grkztl | 317 | pool1qqqz9vlskay2gv3ec5pyck8c2tq9ty7dpfm60x8shvapguhcemt | 211 | 3 | 2020-08-06 20:00:11
610+
```
611+
---
612+
613+
572614
[Query.hs]: https://github.com/input-output-hk/cardano-db-sync/blob/master/cardano-db/src/Cardano/Db/Query.hs

0 commit comments

Comments
 (0)