删除具有相同值的列中的副本?

时间:2014-02-17 09:32:34

标签: sql-server-2008

   url                          link
1.247appliances.co.uk   info@247appliances.co.uk info@247appliances.co.uk
2.365electrical.com sales@365electrical.com sales@365electrical.com sales@365electrical.com sales@365electrical.com|customerservices@365electrical.com sales@365electrical.com

在上表第一行和第二行链接列中有重复值,但需要的结果是

  url                          link
1.247appliances.co.uk   info@247appliances.co.uk 
2.365electrical.com sales@365electrical.com customerservices@365electrical.com 

2 个答案:

答案 0 :(得分:0)

在此处调用列时使用DISTINCT是示例

SELECT DISTINCT column_name,column_name
FROM table_name;

答案 1 :(得分:0)

这可能不是您问题的完全解决方案,但我试图给您一个想法。 干杯

首先创建一个分割函数

CREATE FUNCTION dbo.Split (@sep CHAR(1), @s VARCHAR(512))
RETURNS TABLE
AS
RETURN (
    WITH Pieces(pn, START, stop) AS (
      SELECT 1, 1, CHARINDEX(@sep, @LinkValue)
      UNION ALL
      SELECT pn + 1, stop + 1, CHARINDEX(@sep, @LinkValue, stop + 1)
      FROM Pieces
      WHERE stop > 0
    )
    SELECT pn,
      SUBSTRING(@LinkValue, START, CASE WHEN stop > 0 THEN stop-START ELSE 512 END) AS Link
    FROM Pieces
  )

我已经为样本数据创建了一个临时表(使用你自己的表)

CREATE TABLE #RemoveDuplicateWords (URL VARCHAR(200), Link VARCHAR(200))
  GO
 INSERT INTO #RemoveDuplicateWords(URL,Link)
 SELECT '1.247appliances.co.uk',   'info@247appliances.co.uk info@247appliances.co.uk'
  UNION ALL
SELECT '2.365electrical.com','sales@365electrical.com sales@365electrical.com sales@365electrical.com'
GO

最后一个SELECT查询

SELECT 

    rd.URL,
    st.Link

FROM #RemoveDuplicateWords rd
CROSS APPLY dbo.Split(' ',rd.LINK) AS st
GROUP BY
    rd.URL,
    st.Link