SQL删除几乎重复的行 [英] SQL Remove almost duplicate rows

查看:136
本文介绍了SQL删除几乎重复的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格包含不幸的坏数据,我试图过滤一些。我确信LName,FName组合是唯一的,因为数据集足够小以验证。

  LName,FName,Email 
----- ----- -----
史密斯Bob bsmith@example.com
史密斯鲍勃NULL
Doe Jane NULL
白唐dwhite@example.com

我想让查询结果带回重复记录没有空电子邮件,但如果没有重复的话,仍然会带回NULL电子邮件。



例如

  Smith Bob bsmith@example.com 
Doe Jane NULL
White Don dwhite@example.com

我认为该解决方案类似于 Sql,通过值删除重复行,但是我不太明白asker的要求是否与我的相同。



任何建议?



谢谢

解决方案

如果有任何非空值

  SELECT lname 
,fname
,MIN(电子邮件)
FROM YourTable
GROUP BY
lname
,fname

测试脚本

  DECLARE @Test TABLE(
LName VARCHAR(32)
,FName VARCHAR(32)
,电子邮件VARCHAR(32)


INSERT INTO @Test
SELECT'Smith','Bob','bsmith@example.com '
UNION ALL SELECT'Smith','Bob','NULL'
UNION ALL SELECT'Doe','Jane','NULL'
UNION ALL SELECT'White','Don ','dwhite@example.com'

SELECT lname
,fname
,MIN(电子邮件)
FROM @Test
GROUP BY
lname
,fname


I have a table that contains unfortuantely bad data and I'm trying to filter some out. I am sure that the LName, FName combonation is unique since the data set is small enough to verify.

LName, FName, Email
-----  -----  -----
Smith  Bob    bsmith@example.com
Smith  Bob    NULL
Doe    Jane   NULL
White  Don    dwhite@example.com

I would like to have the query results bring back the "duplicate" record that does not have a NULL email, yet still bring back a NULL Email when there is not a duplicate.

E.g.

Smith Bob   bsmith@example.com
Doe   Jane  NULL
White Don   dwhite@example.com

I think the solution is similar to Sql, remove duplicate rows by value, but I don't really understand if the asker's requirements are the same as mine.

Any suggestions?

Thanks

解决方案

This drops the null rows if there are any non null values.

SELECT  lname
        , fname
        , MIN(email)
FROM    YourTable
GROUP BY
        lname
        , fname

Test script

DECLARE @Test TABLE (
  LName VARCHAR(32)
  , FName VARCHAR(32)
  , Email VARCHAR(32)
)

INSERT INTO @Test
  SELECT 'Smith', 'Bob', 'bsmith@example.com'
  UNION ALL SELECT 'Smith', 'Bob', 'NULL'
  UNION ALL SELECT 'Doe', 'Jane', 'NULL'
  UNION ALL SELECT 'White', 'Don', 'dwhite@example.com'

SELECT  lname
        , fname
        , MIN(Email)        
FROM    @Test
GROUP BY
        lname
        , fname

这篇关于SQL删除几乎重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆