Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何找到部分重复项?_Sql_Duplicates - Fatal编程技术网

Sql 如何找到部分重复项?

Sql 如何找到部分重复项?,sql,duplicates,Sql,Duplicates,我有一个包含两个NVARCHAR列的表:source和target 我想找到可以找到另一行的行,该行具有相同的源和包含当前行的目标 在下面的示例中,我想查找第1行和第7行: 第1行是第3行的部分重复 第7行是第6行的部分重复 下面是一段SQL代码: CREATE TABLE #YourTable (ID int, [source] nvarCHAR(12), [target] nvarCHAR(12)) INSERT INTO #YourTable ([ID],[source],[target

我有一个包含两个NVARCHAR列的表:source和target

我想找到可以找到另一行的行,该行具有相同的源和包含当前行的目标

在下面的示例中,我想查找第1行和第7行:

第1行是第3行的部分重复 第7行是第6行的部分重复 下面是一段SQL代码:

CREATE TABLE #YourTable (ID int, [source] nvarCHAR(12), [target] nvarCHAR(12))

INSERT INTO #YourTable ([ID],[source],[target]) VALUES (1,'wordA','word1')
INSERT INTO #YourTable ([ID],[source],[target]) VALUES (2,'wordA','word2')
INSERT INTO #YourTable ([ID],[source],[target]) VALUES (3,'wordA','word3 ; word1')
INSERT INTO #YourTable ([ID],[source],[target]) VALUES (4,'wordB','word4')
INSERT INTO #YourTable ([ID],[source],[target]) VALUES (5,'wordC','word5')
INSERT INTO #YourTable ([ID],[source],[target]) VALUES (6,'wordD','word6 ; word7')
INSERT INTO #YourTable ([ID],[source],[target]) VALUES (7,'wordD','word7')

SELECT 
  [source],
  STUFF((
    SELECT ', ' + [target]
    FROM #YourTable 
    WHERE ([source] = Results.[source]) 
    FOR XML PATH (''))
  ,1,2,'') AS NameValues
FROM #YourTable Results
GROUP BY [source]
HAVING COUNT(1)>1

DROP TABLE #YourTable
我的第一个想法是连接,但这并没有让我更接近解决方案

我可以将数据导出到CSV,并使用编程语言python、C、。。。隔离ID,但我很想看看如何在SQL中实现


最终目标是删除部分重复项。

您的工作可以使用exists操作符完成:

delete
  from #yourtable   t1
 where exists (
          select 1
            from #yourtable t2
           where t2.source = t1.source
             and t2.target <> t1.target
             and t2.target like t1.target || '%'
       )
     ;

可以使用exists运算符完成您的工作:

delete
  from #yourtable   t1
 where exists (
          select 1
            from #yourtable t2
           where t2.source = t1.source
             and t2.target <> t1.target
             and t2.target like t1.target || '%'
       )
     ;
我将和t2.target类似于t1.target | |“%”改为和t2。[target]类似于“%”+t1。[target]+“%”或t2。[target]类似于t1。[target]+“%”或t2。[target]类似于“%”+t1。[target]和t2。[target]类似于“%”+t1。[target]+“%”将满足要求,因为“%”与空字符串相匹配,这是在clauseI更改的位置内具有和t2.target t1.target,并且t2.target类似于t1.target | |‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘+“%”将满足要求,因为“%”与空字符串匹配,这是where子句中包含和t2.target t1.target的原因