从SQL表中删除重复项
示例: i / p:
EID EName .... ERole
1 Nani SQL
2 Nani SQL Developer
3 Suresh ASP .Net Developer
4 Suresh ASP .Net
5 Ravi Sales Force
6 Ravi Sales Force developer
我的数据设置如上,
输出:
EID EName .... ERole
2 Nani SQL Developer
3 Suresh ASP .Net Developer
6 Ravi Sales Force developer
上面的注释是示例:
在ERole
列中,如果前10个字符匹配,则应重复。
答案 0 :(得分:2)
首先,我将设置您的示例,以便您对其进行测试。
CREATE TABLE #example
(
EID INT PRIMARY KEY IDENTITY,
EName VARCHAR(100),
ERole VARCHAR(MAX)
)
INSERT INTO
#example
VALUES
('Nani','SQL'),
('Nani','SQL Developer'),
('Suresh','ASP .Net Developer'),
('Suresh','ASP .Net'),
('Ravi','Sales Force'),
('Ravi','Sales Force developer')
现在,如果您想根据自己的条件删除重复的记录(相同的名称,在ERole中前10个字符相同),您可以使用以下方法:
WITH grouped_example as (
SELECT
ROW_NUMBER() OVER(PARTITION BY Ename, SUBSTRING(ERole,0,10) ORDER BY ERole DESC) as preserve,
EID
FROM #example
)
DELETE FROM grouped_example where preserve <> 1
如果您要选择无重复的SELECT,则可以使用以下方法:
WITH grouped_example as (
SELECT
ROW_NUMBER() OVER(PARTITION BY Ename, SUBSTRING(ERole,0,10) ORDER BY ERole DESC) as preserve,
*
FROM #example
)
SELECT EID,EName,ERole FROM grouped_example where preserve = 1
*注意:我使用ORDER BY ERole DESC,以便我们继续担任该角色,以获取更多信息(更多字符) *注2:您可以更改需要匹配的字符数,更改SUBSTRING()的最后一个值
答案 1 :(得分:1)
在ERole列中,如果前10个字符匹配,则应重复
WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY LEFT(EROle, 10) ORDER BY EID) RN
FROM T
)
DELETE T
FROM CTE INNER JOIN T
ON CTE.EID = T.EID
WHERE RN > 1;
答案 2 :(得分:0)
根据数据集,如果名称仅限于一种角色,则下面的方法将起作用
Device_group
答案 3 :(得分:0)
对于您的数据,您可以使用:
select t.*
from t
where not exists (select 1
from t t2
where t2.ename = t.ename and
t.erole like t2.erole + '%' and
t.eid < t2.eid
);
这不包括“前十个字符”的限制。但我怀疑它确实可以满足您的要求。
答案 4 :(得分:0)
查看数据集后,这应该对您有用!
CREATE TABLE test
(
EID int,
ENAME VARCHAR(20),
EROLE VARCHAR(30));
INSERT INTO test
VALUES (1,'NANI','SQL'),
(2,'NANI','SQL DEVELOPER'),
(3,'Suresh','ASP .NET Developer'),
(4,'Suresh', 'ASP .Net'),
(5, 'Ravi', 'Sales Force'),
(6, 'Ravi', 'Sales Force developer');
DELETE FROM test
WHERE EID IN(
SELECT EID FROM (
SELECT *, ROW_NUMBER() OVER (PARTITION BY ENAME ORDER BY LEN(EROLE) DESC) as RN
from #test) tab1 WHERE RN != 1);
SELECT * FROM test
答案 5 :(得分:0)
尝试一下
with cte as
(
select *,row_number()over(partition by LEFT(EROle, 10) order by left(EROle, 10) rn
from T
) delete from cte where rn=1