sql语句求解,我不知道这种写法要怎么写,高手帮忙

CQP 2009-09-15 05:26:54
问题描述:
我的一张表的内容如下:
F_SORT_ID,F_CHILD_ID,F_ACT_ID
1 4 1
2 4 1
3 4 1
4 1 2
5 3 4

我想通过SQL语句把他排成这种格式
F_SORT_ID,F_CHILD_ID,F_ACT_ID
1 1 2
2 4 1
3 4 1
4 4 1
5 3 4

也就是说F_CHILD_ID依赖于F_ACT_ID存在,先有F_ACT_ID才有F_CHILD_ID
怎么实现,望高手帮忙,本人不盛感激!!
...全文
182 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
feixianxxx 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 libin_ftsafe 的回复:]
SQL codecreatetable t1(F_CHILD_IDint,F_ACT_IDint)insertinto t1select4,1insertinto t1select4,1insertinto t1select4,1insertinto t1select1,2insertinto t1select3,4go--创建用户定义函数createfunction f_getNum(@F_AC¡­
[/Quote]\



学习
guguda2008 2009-09-15
  • 打赏
  • 举报
回复
回家一看已经有答案了,那就学习好了
fanzhouqi 2009-09-15
  • 打赏
  • 举报
回复

declare @t table(F_CHILD_ID int,F_ACT_ID int)
insert into @t
select 4, 1
union all select 4, 1
union all select 4, 1
union all select 1, 2
union all select 3, 4

if object_id('tempdb..#')is not null
drop table #

select * into #
from @t

alter table # add id int identity(1,1)

if object_id('tempdb..#1')is not null
drop table #1
go
select
a.*
,bid = b.id
into #1
from # a
left join # b
on a.f_act_id = b.f_child_id
order by b.f_child_id


select *
from #1 a
where not exists(select 1 from #1 where id = a.id and bid < a.bid)

F_CHILD_ID F_ACT_ID id bid
----------- ----------- ----------- -----------
1 2 4 NULL
4 1 1 4
4 1 2 4
4 1 3 4
3 4 5 1

(所影响的行数为 5 行)
子陌红尘 2009-09-15
  • 打赏
  • 举报
回复
create table t1(F_CHILD_ID int,F_ACT_ID int)
insert into t1 select 4,1
insert into t1 select 4,1
insert into t1 select 4,1
insert into t1 select 1,2
insert into t1 select 3,4
go

--创建用户定义函数
create function f_getNum(@F_ACT_ID int)
returns varchar(4000)
as
begin
declare @ret varchar(4000),@F_CHILD_ID int
set @ret = right('0000'+rtrim(@F_ACT_ID),4)
while exists(select 1 from t1 where F_CHILD_ID=@F_ACT_ID)
begin
select @F_CHILD_ID=F_ACT_ID from t1 where F_CHILD_ID=@F_ACT_ID
set @F_ACT_ID = @F_CHILD_ID
set @ret = right('0000'+rtrim(@F_ACT_ID),4)+@ret
end
return @ret
end
go

--执行查询
select
a.F_CHILD_ID,
a.F_ACT_ID
from
t1 a
order by
dbo.f_getNum(a.F_ACT_ID)

/*
F_CHILD_ID F_ACT_ID
----------- -----------
1 2
4 1
4 1
4 1
3 4
*/
go

drop function f_getNum
drop table t1
go
黄_瓜 2009-09-15
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 htl258 的回复:]
SQL code/*---------------------------------
-- Author : htl258(Tony)
-- Date : 2009-09-15 18:45:44
-- Version: Microsoft SQL Server 2000 - 8.00.2039 (Intel X86)
May 3 2005 23:18:38
Co¡­
[/Quote]
学习
htl258_Tony 2009-09-15
  • 打赏
  • 举报
回复

/*---------------------------------
-- Author : htl258(Tony)
-- Date : 2009-09-15 18:45:44
-- Version: Microsoft SQL Server 2000 - 8.00.2039 (Intel X86)
May 3 2005 23:18:38
Copyright (c) 1988-2003 Microsoft Corporation
Enterprise Edition on Windows NT 5.1 (Build 2600: Service Pack 3)

---------------------------------*/
--> 生成测试数据表:tb

IF NOT OBJECT_ID('[tb]') IS NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb]([F_CHILD_ID] INT,[F_ACT_ID] INT)
INSERT [tb]
SELECT 4,1 UNION ALL
SELECT 4,1 UNION ALL
SELECT 4,1 UNION ALL
SELECT 1,2 UNION ALL
SELECT 3,4
GO
--SELECT * FROM [tb]

-->SQL查询如下:

declare @t table(F_SORT_ID int identity,[F_CHILD_ID] int,[F_ACT_ID] INT,lvl int)
declare @lvl int
set @lvl=0
insert @t select *,@lvl from tb t where not exists(select 1 from tb where F_CHILD_ID=t.F_ACT_ID)
while @@rowcount>0
begin
set @lvl=@lvl+1
insert @t select *,@lvl from tb a where exists(select 1 from @t where a.F_ACT_ID=F_CHILD_ID and lvl=@lvl-1)
end
select F_SORT_ID,F_CHILD_ID,F_ACT_ID from @t
/*
F_SORT_ID F_CHILD_ID F_ACT_ID
----------- ----------- -----------
1 1 2
2 4 1
3 4 1
4 4 1
5 3 4

(所影响的行数为 5 行)
*/
黄_瓜 2009-09-15
  • 打赏
  • 举报
回复
明白你的意思,但是不会写
CQP 2009-09-15
  • 打赏
  • 举报
回复
先谢了!
guguda2008 2009-09-15
  • 打赏
  • 举报
回复
我看懂了,LZ=我回家给你写
CQP 2009-09-15
  • 打赏
  • 举报
回复
就是个树形的关系,是依赖关系
DengXingJie 2009-09-15
  • 打赏
  • 举报
回复
還真沒看懂樓主的意思
CQP 2009-09-15
  • 打赏
  • 举报
回复
其实就是一个依赖关系,望高手帮忙!!!
CQP 2009-09-15
  • 打赏
  • 举报
回复
问题描述:
我的一张表的内容如下:
F_CHILD_ID,F_ACT_ID
4 1
4 1
4 1
1 2
3 4

我想通过SQL语句把他排成这种格式
F_CHILD_ID,F_ACT_ID
1 2
4 1
4 1
4 1
3 4

也就是说F_CHILD_ID依赖于F_ACT_ID存在,先有F_ACT_ID才有F_CHILD_ID
怎么实现,望高手帮忙,本人不盛感激!!
大家可以不用考虑F_SORT_ID情况
CQP 2009-09-15
  • 打赏
  • 举报
回复
F_SORT_ID是自增列,自动生成的
yylovelij 2009-09-15
  • 打赏
  • 举报
回复
F_SORT_ID这一列是主键还是其他的。
lz排列之后和以前的F_SORT_ID不一样啊。
希望lz详细说明下这个表,我们也好解答啊。
好像都不怎么明白~~~~
jinjazz 2009-09-15
  • 打赏
  • 举报
回复
你确信这是再玩数据库?
小宏 2009-09-15
  • 打赏
  • 举报
回复
关注。。。。
soft_wsx 2009-09-15
  • 打赏
  • 举报
回复
我想通过SQL语句把他排成这种格式
F_SORT_ID,F_CHILD_ID,F_ACT_ID
1 1 2
2 4 1
3 4 1
4 4 1
5 3 4
最大排中间 最小排中间
--小F-- 2009-09-15
  • 打赏
  • 举报
回复
什么排序规则???给个规则》。。
guguda2008 2009-09-15
  • 打赏
  • 举报
回复
看明白了
加载更多回复(2)

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧