MS SQL Server存在相同的行

时间:2017-08-03 08:02:55

标签: sql database sql-server-2008

我有这样的表

TABLE1

DATE                     NAME  PRODUCT    VERSION       Colour    SIZE    ID
2017-08-03 01:30:20.000  Bob   Mouse      12            Pink      3      461
2017-08-03 01:30:20.000  Bob   Mouse      12            Pink      3      446
2017-08-03 01:30:20.000  Bob   Mouse      13            Pink      3      487
2017-08-03 01:30:20.000  Bob   Honey      6             Red       5      476
2017-08-03 01:30:20.000  Bob   Honey      6             Blue      5      774
2017-08-03 01:30:20.000  Bob   Shoe       6             Black     5      487
2017-08-03 01:30:20.000  Bob   Dog        5             Black     7      1874
2017-08-03 01:30:20.000  Bob   Dog        5             Black     7      999
2017-08-03 01:30:20.000  Bob   Pet        689           Red       9      855
2017-08-02 01:30:20.000  Eva   Mouse      12            Pink      3      461
2017-08-02 01:30:20.000  Eva   Mouse      12            Pink      3      446

结果应如下所示:

TABLE1

DATE                     NAME  PRODUCT    VERSION       Colour    SIZE    ID
2017-08-03 01:30:20.000  Bob   Mouse      12            Ping      3      446
2017-08-03 01:30:20.000  Bob   Mouse      13            Ping      3      487
2017-08-03 01:30:20.000  Bob   Honey      6             Red       5      476
2017-08-03 01:30:20.000  Bob   Shoe       6             Black     5      487
2017-08-03 01:30:20.000  Bob   Dog        5             Black     7      1874
2017-08-03 01:30:20.000  Bob   Pet        689           Red       9      855
2017-08-02 01:30:20.000  Eva   Mouse      12            Pink      3      446

我感兴趣的主要是:ProductVersion

如果不同,则应删除包含相同PRODUCT AND VERSION

的行的其他内容

问题出在ID列,我的数字总是不同ID,但是NAME不同我有ID

1 个答案:

答案 0 :(得分:0)

如果您只需要选择此类数据或需要删除它,可以使用以下示例:

create table #test
(
   [DATE] DateTime2(7),
   [NAME] VARCHAR(200),
   [PRODUCT] VARCHAR(200),
   [VERSION] INT,
   [Colour] VARCHAR(200),
   [SIZE] INT,
   [ID] INT
);

INSERT INTO #test VALUES ('2017-08-03 01:30:20.000',  'Bob',   'Mouse',      12,            'Pink',      3,      461);
INSERT INTO #test VALUES ('2017-08-03 01:30:20.000',  'Bob',   'Mouse',      12,            'Pink',      3,      446);
INSERT INTO #test VALUES ('2017-08-03 01:30:20.000',  'Bob',   'Mouse',      13,            'Pink',      3,      487);
INSERT INTO #test VALUES ('2017-08-03 01:30:20.000',  'Bob',   'Honey',      6,             'Red',       5,      476);
INSERT INTO #test VALUES ('2017-08-03 01:30:20.000',  'Bob',   'Honey',      6,             'Blue',      5,      774);
INSERT INTO #test VALUES ('2017-08-03 01:30:20.000',  'Bob',   'Shoe',       6,             'Black',     5,      487);
INSERT INTO #test VALUES ('2017-08-03 01:30:20.000',  'Bob',   'Dog',        5,             'Black',     7,      1874);
INSERT INTO #test VALUES ('2017-08-03 01:30:20.000',  'Bob',   'Dog',        5,             'Black',     7,      999);
INSERT INTO #test VALUES ('2017-08-03 01:30:20.000',  'Bob',   'Pet',        689,           'Red',       9,      855);
INSERT INTO #test VALUES ('2017-08-02 01:30:20.000',  'Eva',   'Mouse',      12,            'Pink',      3,      461);
INSERT INTO #test VALUES ('2017-08-02 01:30:20.000',  'Eva',   'Mouse',      12,            'Pink',      3,      446);


SELECT 
   [DATE] ,
   [NAME],
   [PRODUCT],
   [VERSION],
   [Colour],
   [SIZE],
   [ID] 
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY [PRODUCT], [VERSION] ORDER BY [ID]) rn
FROM #test
) a
WHERE rn = 1;


DELETE FROM #test WHERE ID IN (SELECT 
   [ID] 
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY [PRODUCT], [VERSION] ORDER BY [ID]) rn
FROM #test
) a
WHERE rn > 1);

SELECT * FROM #test;

DROP TABLE #test;