如何去掉重复值的问题

icystone 2007-10-10 10:27:20
关于循环担保的问题,数据如下:
客户 担保人
a b
b a
c d
d e
e c

检索二级循环担保的语句如下:
select table1.客户,table2.客户 from table table1, table table2 where table1.客户=table2.担保人
结果为两条:
a b
b a
这是两条重复记录,可以通过如下方式解决
select table1.客户,table2.客户 from table table1, table table2 where table1.客户=table2.担保人 and table1.客户 〉table2.客户



检索三级循环担保的结果为三条:
c d e
d e c
e c d
请问如何去掉这里的重复值
...全文
347 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiuming0306 2007-10-19
  • 打赏
  • 举报
回复
函数和存储过程没什么大的区别,限制还特别多!


create table b(ke varchar(5),dan varchar(5))
insert b
select 'a', 'b'
union all select 'b', 'a'
union all select 'c', 'd'
union all select 'd', 'e'
union all select 'e', 'c'

select
b1.ke,
b2.ke,
b3.ke
from
b b1,
b b2,
b b3
where
b1.dan =b2.ke
and b2.dan<>b1.ke
and b2.dan=b3.ke
and b3.dan<>b2.ke
and b1.ke>b2.ke
----结果
e c d
samfeng_2003 2007-10-19
  • 打赏
  • 举报
回复
刚才写了个递归函数感觉反而把问题弄复杂了,因为存在a,b--b,a这样的数据,清理后效率是很低的,所以就想到了对比字段上,可能不太好,见量!

create table b(ke varchar(5),dan varchar(5))
insert b
select 'a','b' union all
select 'b','a' union all
select 'c','d' union all
select 'd','e' union all
select 'e','c' union all
select '1','2' union all
select '2','1' union all
select '3','4' union all
select '4','5' union all
select '5','3'
union all
select '11','22' union all
select '22','33' union all
select '33','44' union all
select '44','11'

GO

--来得到合并的字符串的ASCII码的数字
CREATE FUNCTION F_UNIONTOTAL(@s VARCHAR(100))
RETURNS INT
AS
BEGIN
DECLARE @re INT
SELECT @s = RTRIM(LTRIM(@s)),@re = 0
WHILE @s<>''
BEGIN
SET @re = @re + ASCII(LEFT(@s,1))
SET @s = STUFF(@s,1,1,'')
END
RETURN @re
END
GO

--两级匹配
SELECT One,Two FROM
(
select One=Oa.ke,Two=Ob.ke,Three=dbo.F_UNIONTOTAL(Oa.ke+Ob.ke)
from b Oa ,b Ob WHERE Oa.dan = Ob.ke
) A WHERE One + Two IN
(
SELECT Top 1 One+Two FROM
(
select One=Oa.ke,Two=Ob.ke,Three=dbo.F_UNIONTOTAL(Oa.ke+Ob.ke)
from b Oa ,b Ob WHERE Oa.dan = Ob.ke
) B WHERE A.Three = B.Three
)

--三级匹配
SELECT One,Two,Three FROM
(
select One=Oa.ke,Two=Oa.dan,Three=Ob.dan,Four=dbo.F_UNIONTOTAL(Oa.ke+Oa.dan+Ob.dan)
from b Oa ,b Ob WHERE Oa.dan = Ob.ke AND dbo.F_UNIONTOTAL(Oa.ke+Oa.dan) <> dbo.F_UNIONTOTAL(Ob.ke+Ob.dan)
) A WHERE One + Two + Three IN
(
SELECT TOP 1 One + Two + Three FROM
(
select One=Oa.ke,Two=Oa.dan,Three=Ob.dan,Four=dbo.F_UNIONTOTAL(Oa.ke+Oa.dan+Ob.dan)
from b Oa ,b Ob WHERE Oa.dan = Ob.ke AND dbo.F_UNIONTOTAL(Oa.ke+Oa.dan) <> dbo.F_UNIONTOTAL(Ob.ke+Ob.dan)
) B WHERE A.Four = B.Four
)



DROP FUNCTION F_UNIONTOTAL
DROP TABLE b



One Two
----- -----
11 22
2 1
3 4
33 44
4 5
44 11
5 3
b a
c d
d e
e c

(所影响的行数为 11 行)

One Two Three
----- ----- -----
e c d
5 3 4
22 33 44
33 44 11
44 11 22

(所影响的行数为 5 行)
icystone 2007-10-18
  • 打赏
  • 举报
回复
函数该怎样写,我想了好久也没有想明白,请哪位高人给点提示!
Limpire 2007-10-14
  • 打赏
  • 举报
回复
我的方法行不通的,再想想先
Limpire 2007-10-14
  • 打赏
  • 举报
回复
嗯,是不能通过A<B的方式解决,再想想先
Limpire 2007-10-14
  • 打赏
  • 举报
回复
--三级循环担保
select a.担保人,b.担保人,c.担保人
from @T a
join @T b on a.客户=b.担保人 and a.担保人<>b.客户
join @T c on b.客户=c.担保人 and b.担保人<>c.客户 and a.担保人=c.客户
--为更严谨,增加一个条件:b.担保人<>c.客户
where a.担保人<b.担保人

--四级循环担保
select a.担保人,b.担保人,c.担保人,d.担保人
from @T a
join @T b on a.客户=b.担保人 and a.担保人<>b.客户
join @T c on b.客户=c.担保人 and b.担保人<>c.客户
join @T d on c.客户=d.担保人 and c.担保人<>d.客户 and a.担保人=d.客户
--为更严谨,增加一个条件:c.担保人<>d.客户
where a.担保人<b.担保人
icystone 2007-10-14
  • 打赏
  • 举报
回复
就这里的数据 我要的结果是
c d e
d e c
e c d

这三条中任意一条
Limpire 2007-10-14
  • 打赏
  • 举报
回复

/*
就是不可能通过类似A>B的方式解决这个问题
--------------------
看如何写代码了,下面测试结果,如果不加WHERE部分,返回的是一个矩阵,那就完全可以通过 WHERE A<B 的方式解决问题。
*/

--测试数据:@T
declare @T table(客户 varchar(10),担保人 varchar(10))
insert @T
select 'a','b' union all
select 'b','a' union all
select 'c','d' union all
select 'd','e' union all
select 'e','c' union all
select '1','2' union all
select '2','1' union all
select '3','4' union all
select '4','5' union all
select '5','3'
union all
select '11','22' union all
select '22','33' union all
select '33','44' union all
select '44','11'


--二级循环担保
select a.担保人,b.担保人
from @T a
join @T b on a.客户=b.担保人 and a.担保人=b.客户
where a.担保人<b.担保人
/*
a b
1 2
*/

--三级循环担保
select a.担保人,b.担保人,c.担保人
from @T a
join @T b on a.客户=b.担保人 and a.担保人<>b.客户
join @T c on b.客户=c.担保人 and a.担保人=c.客户
where a.担保人<b.担保人

/*
c e d
3 5 4
*/

--四级循环担保
select a.担保人,b.担保人,c.担保人,d.担保人
from @T a
join @T b on a.客户=b.担保人 and a.担保人<>b.客户
join @T c on b.客户=c.担保人 and b.担保人<>c.客户
join @T d on c.客户=d.担保人 and a.担保人=d.客户
where a.担保人<b.担保人

/*
11 44 33 22
*/
qiuming0306 2007-10-14
  • 打赏
  • 举报
回复

create table b(ke varchar(5),dan varchar(5))
insert b
select 'a', 'b'
union all select 'b', 'a'
union all select 'c', 'd'
union all select 'd', 'e'
union all select 'e', 'c'

select
b1.ke,
b2.ke,
b3.ke
from
b b1,
b b2,
b b3
where
b1.dan =b2.ke
and b2.dan<>b1.ke
and b2.dan=b3.ke
and b3.dan<>b2.ke
and b1.ke>b2.ke

qiuming0306 2007-10-14
  • 打赏
  • 举报
回复
这个问题只能按照特定顺序排列,没有其他方法的
qiuming0306 2007-10-14
  • 打赏
  • 举报
回复
既然是循环担保名就又且只有一组数据是满足关系的,为什么不能用〉来解决
Limpire 2007-10-14
  • 打赏
  • 举报
回复
只能通过函数来解决了。
qiuming0306 2007-10-12
  • 打赏
  • 举报
回复
执行了阿!你要什么结果把结果贴出来。最后这个是不要重复数据!当然没有结果!
你把数据贴出来,然后把结果贴出来!
我再给你写,咱们两个的理解有问题
icystone 2007-10-12
  • 打赏
  • 举报
回复
楼上的兄弟你的代码你执行了吗?
根本就没有结果,并且逻辑也不对
qiuming0306 2007-10-11
  • 打赏
  • 举报
回复
你不想出现循环担保是吗!简单


create table b(ke varchar(5),dan varchar(5))
insert b
select 'a', 'b'
union all select 'b', 'a'
union all select 'c', 'd'
union all select 'd', 'e'
union all select 'e', 'c'

select
b1.ke,
b2.ke,
b3.ke
from
b b1,
b b2,
b b3
where
b1.dan =b2.ke
and b2.dan<>b1.ke
and b2.dan=b3.ke
and b3.dan<>b2.ke
and b3.dan<>b1.ke

mengmou 2007-10-11
  • 打赏
  • 举报
回复
你是想加个约束,避免出现循环担保么?
icystone 2007-10-11
  • 打赏
  • 举报
回复
首先解释一下子我所谓的重复值:
客户D给客户C担保,客户E给客户D担保,客户C给客户E担保.
其结构为

客户 担保人
a b
b a
c d
d e
e c

业务逻辑为选择出三级循环担保的情况,结果为
c d e
d e c
e c d

实际上这三条中只是一个循环担保,所以说重复

还有就是不可能通过类似A>B的方式解决这个问题,因为两个值只相邻一次,具体说来就是只要出现A,B就不会出现B,A




至于说写个函数,或许可行,但请高人给个具体的示例!
qiuming0306 2007-10-10
  • 打赏
  • 举报
回复


create table b(ke varchar(5),dan varchar(5))
insert b
select 'a', 'b'
union all select 'b', 'a'
union all select 'c', 'd'
union all select 'd', 'e'
union all select 'e', 'c'

select
b1.ke,
b2.ke,
b3.ke
from
b b1,
b b2,
b b3
where
b1.dan =b2.ke
and b2.dan<>b1.ke
and b2.dan=b3.ke
and b3.dan<>b2.ke
and b1.ke>b2.ke


select
b1.ke,
b2.ke

from
b b1,
b b2

where
b1.dan =b2.ke
and b2.dan=b1.ke


elvis_gao 2007-10-10
  • 打赏
  • 举报
回复

select b1.ke,b2.ke,b3.ke from b b1, b b2, b b3
where b1.dan=b2.ke and b2.dan=b3.ke and b1.ke>b2.ke and b1.ke>b3.ke
qiuming0306 2007-10-10
  • 打赏
  • 举报
回复


select
b1.ke,
b2.ke,
b3.ke
from
b b1,
b b2,
b b3
where
b1.dan =b2.ke
and b2.dan<>b1.ke
and b2.dan=b3.ke
and b3.dan<>b2.ke
and b1.ke>b2.ke
加载更多回复(8)

34,588

社区成员

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

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