求一sql,先谢谢了
id name parentid
2000 菜系 null
2001 省 null
2002 车位 null
2003 人均消费 null
2004 市 2001
2005 县 2004
现在要执行一sql,得到如下结果
id name parentid
2000 菜系 null
2001 省 null
2004 市 2001
2005 县 2004
2002 车位 null
2003 人均消费 null
问题点数:100、回复次数:9Top
1 楼wgsasd311(自强不息)回复于 2006-03-03 16:54:28 得分 0
select
*
from tb order by case when name='菜系' then 0
when name='省' then 1 else 2 end,case when parentid is nul then 9999
else parentid endTop
2 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2006-03-03 17:00:28 得分 80
在SQL Server 2000里一个SQL恐怕写不出来,借助用户定义函数的实现方式:
--------------------------------------------------------------------------------------------------------------------------------
create table t (id int,name varchar(10),parentid int)
insert into t select 2000,'菜系 ',null
insert into t select 2001,'省 ',null
insert into t select 2002,'车位 ',null
insert into t select 2003,'人均消费',null
insert into t select 2004,'市 ',2001
insert into t select 2005,'县 ',2004
go
create function f_str(@id int)
returns varchar(1000)
as
begin
declare @ret varchar(1000),@pid int
select @pid=parentid,@ret=right('0000'+rtrim(id),5) from t where id=@id
while @pid is not null
begin
set @id=@pid
select @pid=parentid,@ret=right('0000'+rtrim(id),5)+@ret from t where id=@id
end
return @ret
end
go
select * from t order by dbo.f_str(id)
go
/*
id name parentid
----------- ---------- -----------
2000 菜系 NULL
2001 省 NULL
2004 市 2001
2005 县 2004
2002 车位 NULL
2003 人均消费 NULL
*/
drop function f_str
drop table tTop
3 楼lsqkeke(可可)回复于 2006-03-03 17:00:29 得分 0
select
*
from tb
order by case when name='菜系' then 0
when name='省' then 1 else 2 end,
case when parentid is nul then 99999
else parentid end
Top
4 楼mousefog(IT老鼠)回复于 2006-03-03 17:01:40 得分 0
结果不对啊Top
5 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2006-03-03 17:02:31 得分 0
create table t (id int,name varchar(10),parentid int)
insert into t select 2000,'菜系 ',null
insert into t select 2001,'省 ',null
insert into t select 2002,'车位 ',null
insert into t select 2003,'人均消费',null
insert into t select 2004,'市 ',2001
insert into t select 2005,'县 ',2004
go
create function f_str(@id int)
returns varchar(1000)
as
begin
declare @ret varchar(1000),@pid int
select @pid=parentid,@ret=right('0000'+rtrim(id),5) from t where id=@id
while @pid is not null
begin
set @id=@pid
select @pid=parentid,@ret=right('0000'+rtrim(id),5)+@ret from t where id=@id
end
return @ret
end
go
select * from t order by dbo.f_str(id)
go
/*
id name parentid
----------- ---------- -----------
2000 菜系 NULL
2001 省 NULL
2004 市 2001
2005 县 2004
2002 车位 NULL
2003 人均消费 NULL
*/
drop function f_str
drop table tTop
6 楼mousefog(IT老鼠)回复于 2006-03-03 17:02:33 得分 0
不好意思,刚才看的时候下面的还没刷出来,先谢谢了Top
7 楼lsqkeke(可可)回复于 2006-03-03 17:02:54 得分 10
declare @t table(id int,name varchar(10),parentid int)
insert into @t select 2000,'菜系 ',null
insert into @t select 2001,'省 ',null
insert into @t select 2002,'车位 ',null
insert into @t select 2003,'人均消费',null
insert into @t select 2004,'市 ',2001
insert into @t select 2005,'县 ',2004
select
*
from @t
order by case when name='菜系' then 0
when name='省' then 1 else 2 end,
case when parentid is null then 99999
else parentid end
结果:
id name parentid
2000 菜系 NULL
2001 省 NULL
2004 市 2001
2005 县 2004
2002 车位 NULL
2003 人均消费 NULL Top
8 楼wgsasd311(自强不息)回复于 2006-03-03 17:05:22 得分 10
set nocount on
create table t (id int,name varchar(10),parentid int)
insert into t select 2000,'菜系 ',null
insert into t select 2001,'省 ',null
insert into t select 2002,'车位 ',null
insert into t select 2003,'人均消费',null
insert into t select 2004,'市 ',2001
insert into t select 2005,'县 ',2004
go
select
*
from t order by case when name='菜系' then 0
when name='省' then 1 else 2 end,case when parentid is null then 9999
else parentid end
/*
id name parentid
----------- ---------- -----------
2000 菜系 NULL
2001 省 NULL
2004 市 2001
2005 县 2004
2002 车位 NULL
2003 人均消费 NULL
*/
drop table t
Top
9 楼v888(aosgzi)回复于 2006-03-03 17:10:43 得分 0
1楼对地Top




