读书人

统计的SQL

发布时间: 2012-04-02 19:58:59 作者: rapoo

求一个统计的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 cntorder by 1 desc) bwhere a.cnt=b.cnt 

读书人网 >Access

热点推荐