当前位置: 代码迷 >> Sql Server >> union all 排序有关问题
  详细解决方案

union all 排序有关问题

热度:550   发布时间:2016-04-27 12:11:45.0
union all 排序问题
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='登陆者的部门' 的效果,也没有达到这样的目的,请问大家怎么办?非常感谢!

------解决方案--------------------
SQL code
select  * from V_Emp where DepartID='登陆者的部门'ORDER BY  CASE WHEN  GroupID='登陆者所在专业组' THEN 1 ELSE 2 END asc
------解决方案--------------------
try:
SQL code
select top 100 percent * from V_Emp where DepartID='登陆者的部门' and GroupID='登陆者所在专业组'union allselect 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