union all和order by连用,求助高手!

来源:百度知道 编辑:UC知道 时间:2024/06/02 15:21:26
我在sql server2000中写的sql语句,select yxsh,yxsmc,sum(case when nj='2006' then 1 else 0 end) as rs1,sum(case when nj='2007' then 1 else 0 end)as rs2,sum(case when nj='2008' then 1 else 0 end) as rs3,count(*) as zrs from xs_v where xjm='01' group by yxsh,yxsmc union all select '' as yxsh, '全校专业数合计' as yxsmc,sum(case when nj='2006' then 1 else 0 end) as rs1,sum(case when nj='2007' then 1 else 0 end) as rs2,sum(case when nj='2008' then 1 else 0 end) as rs3,count(*) as zrs from xs_v where xjm='01';其中yxsh是院系所号,yxsmc是院系所名称,我要实现按学院来统计人数,最后一条是总数合计。怎样才能实现除最后一条记录外的其他记录按照院系所号排序???本来想在union all前添加order by yxsh,可是无法实现。

有这个union all关键字时order by yxsh放在最后就行了

http://blog.chinaunix.net/u/19637/showart_449965.html

用临时表解决吧:)