我想這你想要做什麼:
select sum(votes) as total, sum(demo*(1-rep)) as demoonly,
sum(rep*(1-demo)) as reponly, sum(demo*rep) as demorep
from (select address, cont(*) as votes,
max(case when voted = 'DEMO' then 1 else 0 end) as demo,
max(case when voted = 'REP' then 1 else 0 end) as rep
from t
group by address
) t
假設你想戶(地址)與一個以上的人:
select sum(votes) as total, sum(demo*(1-rep)) as demoonly,
sum(rep*(1-demo)) as reponly, sum(demo*rep) as demorep
from (select address, count(*) as votes,
max(case when voted = 'DEMO' then 1 else 0 end) as demo,
max(case when voted = 'REP' then 1 else 0 end) as rep
from test4
group by address
having count(distinct name) > 1
) t
在這裏,我假設「地址」是家庭的代理人,因爲數據中沒有家庭字段。
不知道我理解。看起來你有來自7個不同家庭的11票,但只佔4個。你是否不想只返回一次投票結果? – 2012-07-23 16:19:22
是的,在真正的查詢中,我想要返回只投一次的家庭的結果,我只是將這部分留給了這個問題。謝謝 – user973671 2012-07-23 16:36:41