我想这你想要做什么:
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