此图显示两个名为Documents& amp;参考
并且Document的特定记录具有多个引用,并且引用已在其他文档记录中重复。
我尝试了SQL CTE的RECURSIVE方法但没有得到所需的输出。
以下是必需的结果:
当搜索引用时不喜欢“Aaa11”
例如,对于DocID 1,引用将基于搜索Aaa11,Aaa12,Aaa13。由于Aaa1是基于搜索找到的,现在它应该剩下2个引用并在其他文档中搜索这些引用,
例如,它将搜索出现在DocID 2中的Aaa13,现在它将接受DocID 2的所有引用,如Bbb12,Bbc11和Aaa13。由于Aaa13现在与搜索匹配,它将再次搜索剩余的参考号,如Bbb12& Bbc11。
我们看到我们可以在DocID 3中找到记录Bbb12。它将循环到参考文献的E。
结果将是
DocID,参考编号
以下是我尝试过的CTE代码,但它没有提取任何记录:
WITH DocumentsAndTheirReferences
(DocID,
Ref
)
AS
(
SELECT DISTINCT Reference.DocID,Reference.Ref
FROM Reference
UNION ALL
SELECT DocumentsAndTheirReferences.DocID,DocumentsAndTheirReferences.Ref
FROM DocumentsAndTheirReferences
JOIN Reference ON DocumentsAndTheirReferences.DocID = Reference.DocID
WHERE DocumentsAndTheirReferences.DocID > 1
UNION ALL
SELECT DocumentsAndTheirReferences.DocID, DocumentsAndTheirReferences.Ref FROM DocumentsAndTheirReferences JOIN Reference ON DocumentsAndTheirReferences.DocID = Reference.DocID
WHERE Reference.DocID LIKE DocumentsAndTheirReferences.DocID
)
SELECT * FROM DocumentsAndTheirReferences WHERE Ref LIKE '111aa'
ORDER BY DocumentsAndTheirReferences.DocID
OPTION (MAXRECURSION 0)
答案 0 :(得分:0)
萨米,不是使用CTE而是使用WHILE循环我可以创建以下SQL脚本 您是否可以考虑是否适合您
请注意,我没有使用所有数据填充示例表,因此请使用您自己的示例数据在您自己的表上测试脚本
/*
create table Documents (
DocID int identity(1,1),
Name varchar(10),
Des varchar(10)
)
insert into Documents select 'Abc','2222'
insert into Documents select 'Xyz','3333'
insert into Documents select 'Ghi','4444'
insert into Documents select 'Lmn','5555'
insert into Documents select 'Pqr','6666'
insert into Documents select 'Uwe','7777'
create table Reference (
RefID int identity(1,1),
DocID int,
RefNo varchar(10)
)
insert into Reference select 1,'Aaa11'
insert into Reference select 1,'Aaa12'
insert into Reference select 1,'Aaa13'
insert into Reference select 2,'Bbb12'
insert into Reference select 2,'Bbc11'
insert into Reference select 2,'Aaa13'
insert into Reference select 3,'Ccc11'
insert into Reference select 3,'Ccd12'
insert into Reference select 3,'Bbb12'
insert into Reference select 4,'Ddd11'
insert into Reference select 4,'Ddd12'
insert into Reference select 4,'Ccd12'
insert into Reference select 5,'Rrr11'
insert into Reference select 5,'Rrg12'
insert into Reference select 5,'Ddd12'
insert into Reference select 6,'Aab12'
insert into Reference select 6,'Ssb11'
insert into Reference select 6,'Ffg13'
*/
if object_id('tempdb..#Ref','U') is not null
drop table #Ref
declare @cnt int
declare @ref varchar(10) = 'Aaa11'
select RefID, DocID, RefNo into #Ref from Reference where refno = @ref
set @cnt = @@rowcount
while @cnt > 0
begin
set identity_insert #Ref on
insert into #Ref (RefID, DocID, RefNo)
select r.RefID, r.DocID, r.RefNo
from Reference r
inner join #Ref
on #Ref.DocID = r.DocID
where r.RefID not in (select RefID from #Ref )
set @cnt = @@rowcount
if @cnt > 0
begin
insert into #Ref (RefID, DocID, RefNo)
select r.RefID, r.DocID, r.RefNo
from Reference r
inner join #Ref
on #Ref.RefNo = r.RefNo
where r.RefID not in (select RefID from #Ref )
set @cnt = @@rowcount
end
end
set identity_insert #Ref off
select * from #Ref order by RefId
对于变量值' Aaa11',输出如下
实际上,对于大多数值,输出是相同的,只有第6组与其他值分开。当您使用第6组中的值时,您只能在结果集中获得这3行
答案 1 :(得分:0)
DECLARE @Search VARCHAR(255)
SET @Search ='112aa'
SELECT * FROM
(
SELECT DISTINCT
Reference.DocID AS DocID FROM Reference
INNER JOIN Documents o ON
Reference.DocID = o.ID
WHERE (Ref) LIKE '%'+@Search+'%'
) X
LEFT JOIN Reference ON Reference.DocID = X.DocID
以下是根据任何特定RefNo从参考文献中获取重复记录的另一种方法。它将获取DocID及其引用。但仍然从这个输出我想要对此结果执行另一次搜索,以找到更多的引用重复。