接近
以下方法可用于对分隔的值列表进行重复数据删除。
- 使用
REPLACE() 函数将不同的分隔符转换为相同的分隔符。
- 使用
REPLACE()函数注入XML结束和开始标签来创建一个XML片段
- 使用
CAST(expr AS XML)函数将上述片段转换为XML数据类型
- 使用
OUTER APPLY 应用表值函数nodes() 将XML 片段拆分为其组成的XML 标记。这会在单独的行中返回每个 XML 标记。
- 使用
value() 函数仅从XML 标记中提取值,并使用指定的数据类型返回值。
- 在上述值后附加逗号。
- 请注意,这些值是在单独的行中返回的。
DISTINCT 关键字的使用现在删除重复的行(即值)。
- 使用
FOR XML PATH('') 子句将多行中的值连接成一行。
查询
将上述方法放在查询形式中:
SELECT DISTINCT PivotedTable.PivotedColumn.value('.','nvarchar(max)') + ','
FROM (
-- This query returns the following in theDataXml column:
-- <tag>test1</tag><tag>test2</tag><tag>test1</tag><tag>test2</tag><tag>test3</tag><tag>test4</tag><tag>test4</tag><tag>test4</tag>
-- i.e. it has turned the original delimited data into an XML fragment
SELECT
DataTable.DataColumn AS DataRaw
, CAST(
'<tag>'
-- First replace commas with pipes to have only a single delimiter
-- Then replace the pipe delimiters with a closing and opening tag
+ replace(replace(DataTable.DataColumn, ',','|'), '|','</tag><tag>')
-- Add a final set of closing tags
+ '</tag>'
AS XML) AS DataXml
FROM ( SELECT 'test1,test2,test1|test2,test3|test4,test4|test4' AS DataColumn) AS DataTable
) AS x
OUTER APPLY DataXml.nodes('tag') AS PivotedTable(PivotedColumn)
-- Running the query without the following line will return the data in separate rows
-- Running the query with the following line returns the rows concatenated, i.e. it returns:
-- test1,test2,test3,test4,
FOR XML PATH('')
输入和结果
给定输入:
test1,test2,test1|test2,test3|test4,test4|test4
上面的查询会返回结果:
测试1,测试2,测试3,测试4,
注意末尾的逗号。我会把它作为练习留给你删除它。
编辑:重复次数
OP 在评论中请求“我如何获得重复的计数?在单独的列中”。
最简单的方法是使用上述查询但删除最后一行FOR XML PATH('')。然后,计算上述查询中SELECT 表达式返回的所有值和不同值(即PivotedTable.PivotedColumn.value('.','nvarchar(max)'))。所有值的计数与不同值的计数之间的差异是重复值的计数。
SELECT
COUNT(PivotedTable.PivotedColumn.value('.','nvarchar(max)')) AS CountOfAllValues
, COUNT(DISTINCT PivotedTable.PivotedColumn.value('.','nvarchar(max)')) AS CountOfUniqueValues
-- The difference of the previous two counts is the number of duplicate values
, COUNT(PivotedTable.PivotedColumn.value('.','nvarchar(max)'))
- COUNT(DISTINCT PivotedTable.PivotedColumn.value('.','nvarchar(max)')) AS CountOfDuplicateValues
FROM (
-- This query returns the following in theDataXml column:
-- <tag>test1</tag><tag>test2</tag><tag>test1</tag><tag>test2</tag><tag>test3</tag><tag>test4</tag><tag>test4</tag><tag>test4</tag>
-- i.e. it has turned the original delimited data into an XML fragment
SELECT
DataTable.DataColumn AS DataRaw
, CAST(
'<tag>'
-- First replace commas with pipes to have only a single delimiter
-- Then replace the pipe delimiters with a closing and opening tag
+ replace(replace(DataTable.DataColumn, ',','|'), '|','</tag><tag>')
-- Add a final set of closing tags
+ '</tag>'
AS XML) AS DataXml
FROM ( SELECT 'test1,test2,test1|test2,test3|test4,test4|test4' AS DataColumn) AS DataTable
) AS x
OUTER APPLY DataXml.nodes('tag') AS PivotedTable(PivotedColumn)
对于上面显示的相同输入,此查询的输出是:
CountOfAllValues CountOfUniqueValues CountOfDuplicateValues
---------------- ------------------- ----------------------
8 4 4