Skip to content

Commit

Permalink
tally.sql: screen unreigstered voters; fix latest join
Browse files Browse the repository at this point in the history
  - factor out valid_votes view
  • Loading branch information
dckc committed Oct 6, 2020
1 parent 772a04f commit b274832
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/cli/tally.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
with vote_cast as (
select distinct qid, fromAddr, toAddr, amount, timestamp
drop view if exists valid_votes;
create view valid_votes as
with valid_voter as (
select *
from tx
where tx.fromAddr in (select revAddr from voter)
),
vote_cast as (
select distinct qid, fromAddr, toAddr, choice.prop, amount, timestamp
from valid_voter tx
join choice on choice.addr = tx.toAddr
),
latest as (
Expand All @@ -9,14 +16,22 @@ latest as (
group by qid, fromAddr
),
latest_vote as (
select l.qid, l.fromAddr, c.toAddr, l.max_ts
select l.qid, l.fromAddr, c.toAddr, c.prop, c.amount, c.timestamp
from latest l
join vote_cast c on l.qid = c.qid and l.fromAddr = c.fromAddr
join vote_cast c on l.qid = c.qid and l.fromAddr = c.fromAddr and l.max_ts = c.timestamp
)
select choice.qid, replace(prop, 'Addr', '') sentiment, count(distinct v.fromAddr) qty
select * from latest_vote
;

drop view if exists tally;
create view tally as
select choice.qid, replace(choice.prop, 'Addr', '') sentiment, count(distinct v.fromAddr) qty
from choice
join latest_vote v on v.toAddr = choice.addr
group by choice.qid, prop
join valid_votes v on v.toAddr = choice.addr
group by choice.qid, choice.prop
order by choice.qid, qty desc
;

select qid, prop, addr from choice order by qid, prop;
select * from valid_votes order by fromAddr, qid;
select * from tally;

0 comments on commit b274832

Please sign in to comment.