--如果只你考虑一级的关系,不考虑关系嵌套,则这样就可以了--示例数据 declare @t table(料号 int,位置 varchar(10),关系 int) insert @t select 1,null ,null union all select 2,null ,null union all select 3,null ,null union all select 4,'Q743',null union all select 5,'Q751',null union all select 6,'Q751',5 union all select 7,'Q743',null union all select 8,'Q751',null--查询排序 select * from @t order by case when 关系 is null then 料号 else 关系 end ,case when 关系 is null then null else 料号 end/*--测试结果料号 位置 关系 ----------- ---------- ----------- 1 NULL NULL 2 NULL NULL 3 NULL NULL 4 Q743 NULL 5 Q751 NULL 6 Q751 5 7 Q743 NULL 8 Q751 NULL(所影响的行数为 8 行) --*/
--如果要考虑多层嵌套,还是得写函数--示例数据 create table tb(料号 int,位置 varchar(10),关系 int) insert tb select 1,'' ,null union all select 2,'' ,null union all select 3,'' ,null union all select 4,'Q743',3 union all select 5,'Q751',7 union all select 6,'Q751',1 union all select 7,'Q743',2 union all select 8,'Q751',null go--排序函数 create function f_id() returns @re table(料号 int,level int,sid varchar(8000)) as begin declare @l int set @l=0 insert @re select 料号,@l,cast(料号 as varchar) from tb where 关系 is null while @@rowcount>0 and @l<10 begin set @l=@l+1 insert @re select a.料号,@l,b.sid+','+cast(a.料号 as varchar) from tb a,@re b where a.关系=b.料号 and b.level=@l-1 end return end go--调用实现排序查询 select a.* from tb a,f_id() b where a.料号=b.料号 order by b.sidgo--删除测试 drop table tb drop function f_id/*--测试结果料号 位置 关系 ----------- ---------- ----------- 1 NULL 6 Q751 1 2 NULL 7 Q743 2 5 Q751 7 3 NULL 4 Q743 3 8 Q751 NULL(所影响的行数为 8 行) --*/
--为什么不行呢?--示例数据 declare @t table(料号 int,位置 varchar(10),关系 int) insert @t select 1, NULL ,NULL union all select 2, NULL ,NULL union all select 3, NULL ,1 union all select 4,'Q743',NULL union all select 5,'Q751',NULL union all select 6,'Q751',5 union all select 7,'Q743',NULL union all select 8,'Q751',NULL--查询排序 select * from @t order by case when 关系 is null then 料号 else 关系 end ,case when 关系 is null then null else 料号 end/*--测试结果料号 位置 关系 ----------- ---------- ----------- 1 NULL NULL 3 NULL 1 2 NULL NULL 4 Q743 NULL 5 Q751 NULL 6 Q751 5 7 Q743 NULL 8 Q751 NULL(所影响的行数为 8 行) --*/
我知道了,因为我的数据不是null,是空白,是''。 那个怎么办阿~救命阿
--那就这样嘛 select * from @t order by case when 关系='' then 料号 else 关系 end ,case when 关系='' then null else 料号 end
order by isnull(关系,料号),料号
declare @t table(料号 int,位置 varchar(10),关系 int)
insert @t select 1,null ,null
union all select 2,null ,null
union all select 3,null ,null
union all select 4,'Q743',null
union all select 5,'Q751',null
union all select 6,'Q751',5
union all select 7,'Q743',null
union all select 8,'Q751',null--查询排序
select *
from @t
order by case when 关系 is null then 料号 else 关系 end
,case when 关系 is null then null else 料号 end/*--测试结果料号 位置 关系
----------- ---------- -----------
1 NULL NULL
2 NULL NULL
3 NULL NULL
4 Q743 NULL
5 Q751 NULL
6 Q751 5
7 Q743 NULL
8 Q751 NULL(所影响的行数为 8 行)
--*/
create table tb(料号 int,位置 varchar(10),关系 int)
insert tb select 1,'' ,null
union all select 2,'' ,null
union all select 3,'' ,null
union all select 4,'Q743',3
union all select 5,'Q751',7
union all select 6,'Q751',1
union all select 7,'Q743',2
union all select 8,'Q751',null
go--排序函数
create function f_id()
returns @re table(料号 int,level int,sid varchar(8000))
as
begin
declare @l int
set @l=0
insert @re select 料号,@l,cast(料号 as varchar)
from tb where 关系 is null
while @@rowcount>0 and @l<10
begin
set @l=@l+1
insert @re select a.料号,@l,b.sid+','+cast(a.料号 as varchar)
from tb a,@re b where a.关系=b.料号 and b.level=@l-1
end
return
end
go--调用实现排序查询
select a.*
from tb a,f_id() b
where a.料号=b.料号
order by b.sidgo--删除测试
drop table tb
drop function f_id/*--测试结果料号 位置 关系
----------- ---------- -----------
1 NULL
6 Q751 1
2 NULL
7 Q743 2
5 Q751 7
3 NULL
4 Q743 3
8 Q751 NULL(所影响的行数为 8 行)
--*/
1 NULL NULL
2 NULL NULL
3 NULL 1
4 Q743 NULL
5 Q751 NULL
6 Q751 5
7 Q743 NULL
8 Q751 NULL这样的话按照上面的语句还是没有办法排阿~
declare @t table(料号 int,位置 varchar(10),关系 int)
insert @t select 1, NULL ,NULL
union all select 2, NULL ,NULL
union all select 3, NULL ,1
union all select 4,'Q743',NULL
union all select 5,'Q751',NULL
union all select 6,'Q751',5
union all select 7,'Q743',NULL
union all select 8,'Q751',NULL--查询排序
select *
from @t
order by case when 关系 is null then 料号 else 关系 end
,case when 关系 is null then null else 料号 end/*--测试结果料号 位置 关系
----------- ---------- -----------
1 NULL NULL
3 NULL 1
2 NULL NULL
4 Q743 NULL
5 Q751 NULL
6 Q751 5
7 Q743 NULL
8 Q751 NULL(所影响的行数为 8 行)
--*/
那个怎么办阿~救命阿
select *
from @t
order by case when 关系='' then 料号 else 关系 end
,case when 关系='' then null else 料号 end