SQL Query用于删除冗余数据

时间:2016-07-25 06:06:02

标签: sql sql-server

我有一个数据:

enter image description here

ProductID   Language    Vendor  Title   ProductVersionID    Version ColloquialVersion   ProductEditionID
1   English Microsoft Corporation   SQL Server  1   13  NULL    NULL
1   English Microsoft Corporation   SQL Server  2   13.0.0  NULL    NULL
1   English Microsoft Corporation   SQL Server  3   13.0.0.0    NULL    NULL
1   English Microsoft Corporation   SQL Server  4   13.0.0.275  NULL    NULL
2   English Microsoft Corporation   Visual Studio   5   13  NULL    NULL
2   English Microsoft Corporation   Visual Studio   6   13.0.0  NULL    NULL
2   English Microsoft Corporation   Visual Studio   7   13.0.0.0    NULL    NULL
2   English Microsoft Corporation   Visual Studio   8   13.0.0.275  NULL    NULL

我的问题是:

我想创建查询以删除具有类似版本版本的冗余行示例13.0,13.0.0,13.0.0.0对于SQL Server是相同的。所以我想删除版本为13.0,13.0.0,13.0.0.0的行,只留下版本13.0.0.275。

有没有办法使用一些SQL查询来做同样的事情。

请帮忙。

5 个答案:

答案 0 :(得分:1)

有一些方法可以实现这一目标。试试这个:

    DECLARE @t TABLE (
    productid INT
    ,title VARCHAR(15)
    ,versionNo VARCHAR(50)
    )

INSERT INTO @t
VALUES (
    1
    ,'SQL Server'
    ,'13'
    )
    ,(
    1
    ,'SQL Server'
    ,'13.0.0'
    )
    ,(
    1
    ,'SQL Server'
    ,'13.0.0.0'
    )
    ,(
    1
    ,'SQL Server'
    ,'13.0.0.275'
    )
    ,(
    2
    ,'Visual Studio'
    ,'13'
    )
    ,(
    2
    ,'Visual Studio'
    ,'13.0.0'
    )
    ,(
    2
    ,'Visual Studio'
    ,'13.0.0.0'
    )
    ,(
    2
    ,'Visual Studio'
    ,'13.0.0.275'
    )

SELECT *
FROM @t

--1. Approach First
--delete t from @t t join
--(
--  select productid,title, max(versionNo) versionno from @t 
--  group by productid, title 
--) temp on t.productid = temp.productid and t.title = temp.title and t.versionno <> temp.versionno
--2. Approach Second
DELETE t
FROM @t t
JOIN
    --select distinct t. *  from @t t join
    (
    SELECT DISTINCT productid
        ,title
        ,S.a.value('(/H/r)[4]', 'VARCHAR(100)') FourthPostionValue
    FROM (
        SELECT *
            ,CAST(N'<H><r>' + Replace(versionno, '.', '</r><r>') + '</r></H>' AS XML) AS [vals]
        FROM @t
        ) d
    CROSS APPLY d.[vals].nodes('/H/r') S(a)
    ) TEMP ON t.productid = TEMP.productid
    AND t.title = TEMP.title --and isnull(temp.FourthPostionValue, 0) = 0 
    AND charindex(isnull(TEMP.FourthPostionValue, 2), t.versionNo) = 0

SELECT *
FROM @t

答案 1 :(得分:1)

利用ProductVersionID(每ProductID)似乎已经同意您的条件(假设通过查看更多数据来确认)这一事实

DELETE FROM table FROM table T1 
   WHERE ProductVersionID < 
     (SELECT max(ProductVersionID) 
         FROM table T2 WHERE T1.ProductID = T2.ProductID)

修改 这是新规范之后的更新版本

DELETE FROM table 
FROM table T1 
WHERE T1.Version IN  
(
SELECT T1.Version
   FROM table T2 WHERE 
   ((
        (T2.Version = T1.Version+ '.0') 
        OR (T2.Version = T1.Version+ '.0.0')  
        OR (T2.Version = T1.Version+ '.0.0.0')
   )
   AND T2.ProductID = T1.ProductID )

答案 2 :(得分:0)

检查是否使用此选择

获取要删除的行
    SELECT *
FROM my_table AS a
INNER JOIN (
    SELECT vendor
        ,title
        ,max(Version) AS version
    FROM my_table
    GROUP BY vendor
        ,title
    ) t ON t.vendor = a.vendor
    AND t.title = a.title
    AND t.version != a.version

然后

DELETE
FROM my_table AS a
INNER JOIN (
    SELECT vendor
        ,title
        ,max(Version) AS version
    FROM my_table
    GROUP BY vendor
        ,title
    ) t ON t.vendor = a.vendor
    AND t.title = a.title
    AND t.version != a.version

答案 3 :(得分:0)

试试这个:

    SELECT b.productid
    ,a.Title
    ,a.`Language`
    ,a.Vendor
    ,a.max_ver
FROM (
    SELECT max(Version) AS max_ver
        ,Title
        ,`Language`
        ,Vendor
        ,version
    FROM tbl_product
    GROUP BY `Language`
        ,Vendor
        ,Title
    ORDER BY Title
    ) AS a
LEFT JOIN (
    SELECT productid
        ,Title
        ,`Language`
        ,Vendor
        ,version
    FROM tbl_product
    ) AS b ON a.max_ver = b.version
    AND a.Title = b.Title;
祝你好运..

答案 4 :(得分:0)

由于不清楚或试图解决我们的问题,OP没有帮助。

然而,我会再试一次:

如果列Version '13.0.0.0''13.0.0.725'是合法(或至少更官方)的产品版本,并且要删除与此模式不匹配的所有其他记录...然后使用LEN功能。完成一次扫描。

 CREATE TABLE #tableA (
    productid INT
    ,title VARCHAR(15)
    , [Version] VARCHAR(50)
    )

INSERT INTO #tableA
VALUES (1,'SQL Server','13')
     , (1,'SQL Server','13.0.0')
     , (1,'SQL Server','13.0.0.0')
     , (1,'SQL Server','13.0.0.275')
     , (2,'Visual Studio','13')
     , (2,'Visual Studio','13.0.0')
     , (2,'Visual Studio','13.0.0.0')
     , (2,'Visual Studio','13.0.0.275')

SELECT  *
FROM #TableA
WHERE LEN( SUBSTRING(Version
                       , CHARINDEX('.', Version)
                       , LEN(Version) ) 
          )  < 6

这里的关键是确定你想要的长度。 '.0.0.0'长度为6个字符。如果您想保留它,可以添加特殊例外,例如'13'。

其余的我留给你。