一个逻辑问题.....

会飞的小洋洋 2008-09-12 10:59:53
要求:最好拿ms-sql代码,谢谢

输入:
信息1 信息2
中国 北京
亚洲 上海
亚洲 中国
亚洲 天津
... ...
... ...
... ...

结果:
信息1 信息2 信息3 信息4 信息5(不会再有信息6..)
亚洲 中国 北京
亚洲 上海
亚洲 天津
....
....

提示:

情况1
两两信息比较,信息1 如果等于 信息2,那么这两条记录可合并为 信息1 信息2 信息3。例如 “中国 北京”和“亚洲 中国”比较,“中国”=“中国”,所以这两条合并为 ”亚洲 中国 北京“

情况2
”亚洲 上海“的信息1是”亚洲“ ,没有找到信息2是”亚洲“的记录,故不合并,依然为”亚洲 上海“
...全文
201 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lgxyz 2008-09-12
  • 打赏
  • 举报
回复
DECLARE @TB TABLE (INFO1 VARCHAR(10),INFO2 VARCHAR(10))
INSERT INTO @TB SELECT '中国','北京'
UNION ALL SELECT '亚洲','上海'
UNION ALL SELECT '亚洲','中国'
UNION ALL SELECT '亚洲','天津'
UNION ALL SELECT '宇宙','地球'
UNION ALL SELECT '地球','亚洲'
UNION ALL SELECT '地球','欧洲'




SELECT A.INFO1,A.INFO2,INFO3=B.INFO2,INFO4=C.INFO2,INFO5=D.INFO2 INTO #T FROM @TB A
LEFT JOIN @TB B
ON A.INFO2=B.INFO1
LEFT JOIN @TB C
ON B.INFO2=C.INFO1
LEFT JOIN @TB D
ON C.INFO2=D.INFO1


SELECT * FROM #T A
WHERE NOT EXISTS(SELECT 1 FROM #T
WHERE (INFO2=A.INFO1 OR INFO3=A.INFO1 OR INFO4=A.INFO1 OR INFO5=A.INFO1) AND INFO1<>A.INFO1 )
/*
INFO1 INFO2 INFO3 INFO4 INFO5
---------- ---------- ---------- ---------- ----------
宇宙 地球 亚洲 上海 NULL
宇宙 地球 亚洲 中国 北京
宇宙 地球 亚洲 天津 NULL
宇宙 地球 欧洲 NULL NULL

(所影响的行数为 4 行)

*/
会飞的小洋洋 2008-09-12
  • 打赏
  • 举报
回复
.
leo_lesley 2008-09-12
  • 打赏
  • 举报
回复
FYI

create table t(ID varchar(10),  ParentID varchar(10) ,Node varchar(100))
insert t
select '1001' ,null ,'The World'
union all select '1002' ,'1001' ,'Americas '
union all select '1003' ,'1002' ,'United States '
union all select '1004' ,'1003' ,'California '
union all select '1005' ,'1004' ,'Sonoma Valley '
union all select '1006' ,'1001' ,'Europe '
union all select '1007' ,'1006' ,'Bulgaria '
union all select '1008' ,'1006' ,'France '
union all select '1009' ,'1008' ,'Alsace '
union all select '1010' ,'1008' ,'Bordeaux '
union all select '1011' ,'1010' ,'Graves '
union all select '1012' ,'1010' ,'Medoc '
union all select '1013' ,'1012' ,'Bas-Medoc '
union all select '1014' ,'1013' ,'Listrac '
union all select '1015' ,'1013' ,'Pauillac '
union all select '1016' ,'1013' ,'Saint-Estephe '
union all select '1017' ,'1013' ,'Saint-Julien '
union all select '1018' ,'1012' ,'Haut-Medoc '
union all select '1019' ,'1018' ,'Margaux '
union all select '1020' ,'1018' ,'Moulis '
union all select '1021' ,'1006' ,'Germany '
union all select '1022' ,'1021' ,'Rheingau '
union all select '1023' ,'1001' ,'Oceania '
union all select '1024' ,'1023' ,'Australia '
union all select '1025' ,'1024' ,'South Australia '
union all select '1026' ,'1025' ,'Barossa Valley'
union all select '1026' ,'1025' ,'Barossa Valley '

go


DECLARE @t_Level TABLE(ID varchar(10),Level int,Sort varchar(8000))
DECLARE @Level int

SET @Level=0

INSERT @t_Level
SELECT ID,@Level,id
FROM t
WHERE ParentID IS NULL

WHILE @@ROWCOUNT>0
BEGIN
SET @Level=@Level+1
INSERT @t_Level
SELECT a.ID,@Level,b.Sort+a.ID
FROM t a,@t_Level b
WHERE a.ParentID=b.ID
AND b.Level=@Level-1
END

Select id,
p1=(select top 1 node from t where substring(b.Sort,1,4)=id),
p2=(select top 1 node from t where substring(b.Sort,5,4)=id),
p3=(select top 1 node from t where substring(b.Sort,9,4)=id),
p4=(select top 1 node from t where substring(b.Sort,13,4)=id),
p5=(select top 1 node from t where substring(b.Sort,17,4)=id),
p6=(select top 1 node from t where substring(b.Sort,21,4)=id),
p7=(select top 1 node from t where substring(b.Sort,25,4)=id),
p8=(select top 1 node from t where substring(b.Sort,29,4)=id)
From @t_Level b




Drop Table t

/* 结果
1001 The World NULL NULL NULL NULL NULL NULL NULL
1002 The World Americas NULL NULL NULL NULL NULL NULL
1006 The World Europe NULL NULL NULL NULL NULL NULL
1023 The World Oceania NULL NULL NULL NULL NULL NULL
1003 The World Americas United States NULL NULL NULL NULL NULL
1021 The World Europe Germany NULL NULL NULL NULL NULL
1007 The World Europe Bulgaria NULL NULL NULL NULL NULL
1008 The World Europe France NULL NULL NULL NULL NULL
1024 The World Oceania Australia NULL NULL NULL NULL NULL
1004 The World Americas United States California NULL NULL NULL NULL
1009 The World Europe France Alsace NULL NULL NULL NULL
1010 The World Europe France Bordeaux NULL NULL NULL NULL
1022 The World Europe Germany Rheingau NULL NULL NULL NULL
1025 The World Oceania Australia South Australia NULL NULL NULL NULL
1005 The World Americas United States California Sonoma Valley NULL NULL NULL
1011 The World Europe France Bordeaux Graves NULL NULL NULL
1012 The World Europe France Bordeaux Medoc NULL NULL NULL
1026 The World Oceania Australia South Australia Barossa Valley NULL NULL NULL
1026 The World Oceania Australia South Australia Barossa Valley NULL NULL NULL
1013 The World Europe France Bordeaux Medoc Bas-Medoc NULL NULL
1018 The World Europe France Bordeaux Medoc Haut-Medoc NULL NULL
1014 The World Europe France Bordeaux Medoc Bas-Medoc Listrac NULL
1015 The World Europe France Bordeaux Medoc Bas-Medoc Pauillac NULL
1016 The World Europe France Bordeaux Medoc Bas-Medoc Saint-Estephe NULL
1017 The World Europe France Bordeaux Medoc Bas-Medoc Saint-Julien NULL
1019 The World Europe France Bordeaux Medoc Haut-Medoc Margaux NULL
1020 The World Europe France Bordeaux Medoc Haut-Medoc Moulis NULL
dawugui 2008-09-12
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Shined 的回复:]
输入:
宇宙 地球
地球 亚洲
亚洲 中国
中国 北京

输出:
宇宙 地球 亚洲 中国 北京

这样就是信息5了。。。
但是大不过宇宙,小不过城市,所以只到信息5
[/Quote]
是个树的情况吧?
这个是向上查的,你自己改为向下查即可.

/*
标题:查询各节点的父路径函数
作者:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-05-12
地点:广东深圳
*/

/*
原始数据及要求结果如下:
--食品
--水果
--香蕉
--苹果
--蔬菜
--青菜
id pid name
----------- ----------- --------------------
1 0 食品
2 1 水果
3 1 蔬菜
4 2 香蕉
5 2 苹果
6 3 青菜

要求得到各节点的父路径即如下结果:
id pid name 路径
--- --- ----- ---------------
1 0 食品 食品
2 1 水果 食品,水果
3 1 蔬菜 食品,蔬菜
4 2 香蕉 食品,水果,香蕉
5 2 苹果 食品,水果,苹果
6 3 青菜 食品,蔬菜,青菜
*/

create table tb (id int , pid int , name nvarchar(20))
insert into tb values(1 , 0 , '食品')
insert into tb values(2 , 1 , '水果')
insert into tb values(3 , 1 , '蔬菜')
insert into tb values(4 , 2 , '香蕉')
insert into tb values(5 , 2 , '苹果')
insert into tb values(6 , 3 , '青菜')
go

--查询各节点的父路径函数
create function f_pid(@id int) returns varchar(100)
as
begin
declare @re_str as varchar(100)
set @re_str = ''
select @re_str = name from tb where id = @id
while exists (select 1 from tb where id = @id and pid <> 0)
begin
select @id = b.id , @re_str = b.name + ',' + @re_str from tb a , tb b where a.id = @id and a.pid = b.id
end
return @re_str
end
go

select * , dbo.f_pid(id) 路径 from tb order by id

drop table tb
drop function f_pid
会飞的小洋洋 2008-09-12
  • 打赏
  • 举报
回复
rhq12345 2008-09-12
  • 打赏
  • 举报
回复
如果有信息4,信息5的话类似
rhq12345 2008-09-12
  • 打赏
  • 举报
回复

select a.信息1,a.信息2,b.信息2 as 信息3 from table a,table b where a.信息2=b.信息1
union all
select * ,null 信息3 from table where 信息1 not in
(select a.信息2 as 信息3 from table a,table b where a.信息2=b.信息1)
and 信息2 not in
(select a.信息2 as 信息3 from table a,table b where a.信息2=b.信息1)

会飞的小洋洋 2008-09-12
  • 打赏
  • 举报
回复
输入:
宇宙 地球
地球 亚洲
亚洲 中国
中国 北京

输出:
宇宙 地球 亚洲 中国 北京

这样就是信息5了。。。
但是大不过宇宙,小不过城市,所以只到信息5
dawugui 2008-09-12
  • 打赏
  • 举报
回复
那信息4 信息5有什么用?
会飞的小洋洋 2008-09-12
  • 打赏
  • 举报
回复
最简单就是最可行的

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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