尽量不选择同一连接多次我曾尝试使用CHARINDEX()= 0条件的下列方式的检索集群:递归查询问题 - 针对连接
WITH Cluster(calling_party, called_party, link_strength, Path)
AS
(SELECT
calling_party,
called_party,
link_strength,
CONVERT(varchar(max), calling_party + '.' + called_party) AS Path
FROM
monthly_connections_test
WHERE
link_strength > 0.1 AND
calling_party = 'b'
UNION ALL
SELECT
mc.calling_party,
mc.called_party,
mc.link_strength,
CONVERT(varchar(max), cl.Path + '.' + mc.calling_party + '.' + mc.called_party) AS Path
FROM
monthly_connections_test mc
INNER JOIN Cluster cl ON
(
mc.called_party = cl.called_party OR
mc.called_party = cl.calling_party
) AND
(
CHARINDEX(cl.called_party + '.' + mc.calling_party, Path) = 0 AND
CHARINDEX(cl.called_party + '.' + mc.called_party, Path) = 0
)
WHERE
mc.link_strength > 0.1
)
SELECT
calling_party,
called_party,
link_strength,
Path
FROM
Cluster OPTION (maxrecursion 30000)
条件但不符合其目的因为多次选择相同的行。
这里的实际目标是检索选定用户(在示例用户b中)所属的整个连接集群。
EDIT1:
我试图修改查询方式如下:
With combined_users AS
(SELECT calling_party CALLING, called_party CALLED, link_strength FROM dbo.monthly_connections_test WHERE link_strength > 0.1),
related_users1 AS
(
SELECT c.CALLING, c.CALLED, c.link_strength, CONVERT(varchar(max), '.' + c.CALLING + '.' + c.CALLED + '.') path from combined_users c where CALLING = 'a1'
UNION ALL
SELECT c.CALLING, c.CALLED, c.link_strength,
convert(varchar(max),r.path + c.CALLED + '.') path
from combined_users c
join related_users1 r
ON (c.CALLING = r.CALLED) and CHARINDEX(c.CALLING + '.' + c.CALLED + '.', r.path)= 0
),
related_users2 AS
(
SELECT c.CALLING, c.CALLED, c.link_strength, CONVERT(varchar(max), '.' + c.CALLING + '.' + c.CALLED + '.') path from combined_users c where CALLED = 'a1'
UNION ALL
SELECT c.CALLING, c.CALLED, c.link_strength,
convert(varchar(max),r.path + c.CALLING + '.') path
from combined_users c
join related_users2 r
ON c.CALLED = r.CALLING and CHARINDEX('.' + c.CALLING + '.' + c.CALLED, r.path)= 0
)
SELECT CALLING, CALLED, link_strength, path FROM
(SELECT CALLING, CALLED, link_strength, path FROM related_users1 UNION SELECT CALLING, CALLED, link_strength, path FROM related_users2) r OPTION (MAXRECURSION 30000)
为了测试我创建了以下集群查询:
查询上面回复了下面的表格:
a1 a2 1.0000000 .a1.a2.
a11 a13 1.0000000 .a12.a1.a13.a11.
a12 a1 1.0000000 .a12.a1.
a13 a12 1.0000000 .a12.a1.a13.
a14 a13 1.0000000 .a12.a1.a13.a14.
a15 a14 1.0000000 .a12.a1.a13.a14.a15.
a2 a10 1.0000000 .a1.a2.a10.
a2 a3 1.0000000 .a1.a2.a3.
a3 a4 1.0000000 .a1.a2.a3.a4.
a3 a6 1.0000000 .a1.a2.a3.a6.
a4 a8 1.0000000 .a1.a2.a3.a4.a8.
a4 a9 1.0000000 .a1.a2.a3.a4.a9.
该查询明显地返回朝向所选节点和相反方向的连接的连接。问题在于方向的改变:例如,由于方向改变(相对于起始节点),未选择连接a7,a4和a11,a10。
有谁知道如何修改查询以包含所有连接?
谢谢
你可以给一些样本数据和你期望看到什么吗? – 2010-11-02 15:26:16