日期:2014-05-16  浏览次数:20761 次

求一个统计的SQL
就比如说。我一个班投票选三好学生,一个学生必须投四个不同的人。投完之后让你统计出前四名的学生,并列名次的也要取出来(就是说第四名有二个,其它名次一个,那么取出的记录应该是五条)。
也就是说数据库表是有 第一票,第二票,第三票,第四票。四个字段。
请问这SQL在access中如何写??

------解决方案--------------------
SQL code
select a.*
from (
    select X,count(*) as cnt from (
        select num1 as X from test1
        union all
        select num2 from test1
        union all
        select num3 from test1
        union all
        select num4 from test1
    ) group by X
) a , (
select top 4  cnt from (
select count(*) as cnt from (
        select num1 as X from test1
        union all
        select num2 from test1
        union all
        select num3 from test1
        union all
        select num4 from test1
    ) group by X
) group by cnt
order by 1 desc
) b
where a.cnt=b.cnt