sql server2000数据库试图V_Emp(员工试图)已经按照部门、所在专业组、姓名进行了排序(order by DepartID,GroupID,EmpName),如:
ID,EmpName,      DepartID,GroupID,。
001      001姓名             部门1       组1
004      004姓名             部门1       组1
101      101姓名             部门1       组2
103      103姓名             部门1       组2
106      106姓名             部门2       组1
119      119姓名             部门2       组1
121      121姓名             部门2       组1
221      221姓名             部门2       组2
现在,我想在B/S页面中实现如下员工一览表页面效果:默认页面显示登陆者的部门员工,且把登陆者所在专业组的人员信息排在前面,要求所在专业组的人员信息也按姓名排序,排在后面的专业组也要符合专业组、姓名排序规则,计划采用sql如下:
select top 100 percent * from V_Emp where DepartID='登陆者的部门' and GroupID='登陆者所在专业组'
union all
select top 100 percent * from V_Emp where DepartID='登陆者的部门' and GroupID<>'登陆者所在专业组'
但发现这样出来的效果不是想要达到的,排序规则乱了。若改为:
select * from V_Emp where DepartID='登陆者的部门' and GroupID='登陆者所在专业组'
union all
select * from V_Emp where DepartID='登陆者的部门' and GroupID<>'登陆者所在专业组'
这样出现的就是select * from V_Emp where DepartID='登陆者的部门'  的效果,也没有达到这样的目的,请问大家怎么办?非常感谢!

解决方案 »

  1.   

    select  * from V_Emp where DepartID='登陆者的部门'
    ORDER BY  CASE WHEN  GroupID='登陆者所在专业组' THEN 1 ELSE 2 END asc
      

  2.   

    用一個case when 就行了union all--這樣用效率低 
      

  3.   

    try:
    select top 100 percent * from V_Emp where DepartID='登陆者的部门' and GroupID='登陆者所在专业组'
    union all
    select top 100 percent * from V_Emp where DepartID='登陆者的部门' and GroupID<>'登陆者所在专业组'
    order by (case when DepartID='登陆者的部门' then '' else DepartID end),(case when GroupID='登陆者所在专业组' then '' else GroupID end),empName
      

  4.   

    or:
    select * from V_Emp where DepartID='登陆者的部门'
    order by (case when DepartID='登陆者的部门' then '' else DepartID end),
    (case when GroupID='登陆者所在专业组' then '' else GroupID end),empName
      

  5.   

    order by case when