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

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -