1

假设我有两个具有一对多关系的表。我想创建一个视图/查询来显示父行中的所有列并汇总子行中的总计。这是我经常遇到的一种情况,我不禁觉得我错过了一个更好、更明显的解决方案。我使用的一个是针对子表的多个连接,例如:

SELECT vt.*, COALESCE(count(mvAyes.*), 0) as Ayes, COALESCE(count(mvNoes.*), 0) as Noes  FROM VoteTable vt 
LEFT JOIN MemberVote mvAyes on mvAyes.VoteId = vt.Id AND mvAyes.Vote = 'Aye'
LEFT JOIN MemberVote mvNoes on mvNoes.VoteId = vt.Id AND mvNoes.Vote = 'No'
GROUP BY vt.Col1, vt.Col2 ... (all columns in Vote table)

另一种解决方案是使用相关子查询,据我所知,它的效率很低,尽管我认为上面的多重连接解决方​​案也很低效。

那么,有没有我不知道的更好的解决方案?谢谢!

4

1 回答 1

2

是的,有一个更简单的解决方案:

SELECT vt.*,
       sum(case when mv.Vote = 'Aye' then 1 else 0 end) as Ayes,
       sum(case when mv.Vote = 'No' then 1 else 0 end) as Nos
FROM VoteTable vt left outer join
     MemberVote mv
     on mv.VoteId = vt.Id 
GROUP BY vt.Col1, vt.Col2 ... (all columns in Vote table)

您可以通过在子查询中执行 group by 来进一步简化此操作:

select vt.*, mv.ayes, mv.noes
from vt left outer join
     (select VoteId, sum(case when mv.Vote = 'Aye' then 1 else 0 end) as Ayes,
             sum(case when mv.Vote = 'No' then 1 else 0 end) as Nos
      from MemberVote mv
      group by VoteId
     ) mv
     on vt.id = mv.VoteId
于 2012-08-31T17:00:19.593 回答