SQL: detecting differences in sums between two tables? -
it seems simple enough: have 2 votes , want see states have different numbers of votes. individually, it's easy:
select state, sum(votes) votes_a group state; select state, sum(votes) votes_b group state;
how query state, votes_a, votes_b
states have different results?
try join:
select totals_a.state, totals_a.total_votes, totals_b.total_votes ( select state, sum(votes) total_votes votes_a group state ) totals_a join ( select state, sum(votes) total_votes votes_b group state ) totals_b on totals_a.state = totals_b.state totals_a.total_votes <> totals_b.total_votes
note miss states received 0 votes in 1 of tables. fix use full outer join (assuming database supports feature) , check nulls.
Comments
Post a Comment