如何获得独特的领域

时间:2016-11-02 20:50:48

标签: sql sql-server tsql

CName           |   AddressLine
-------------------------------
John Smith      | 999 Somewhereelse
Jane Doe        | 456 Evergreen Terrace
John Black      | 999 Somewhereelse
Joe Bloggs      | 1 Second Ave

我想选择具有唯一AddressLine的CName意味着我不想选择“John Smith”和“John Black”,因为他们有相同的地址。我该怎么做?

4 个答案:

答案 0 :(得分:2)

对于所有非空地址,您可以按地址行分组,只选择那些唯一的地址。

select * from t
where addressline in (select addressline from t 
                      where addressline is not null 
                      group by addressline 
                      having count(*) = 1)
--or addressline is null

select cname,addressline
from (select t.*, count(*) over(partition by addressline) cnt
      from t) x 
where cnt = 1

答案 1 :(得分:1)

您可以不在subselect with group by和

中使用
BRPOP

答案 2 :(得分:0)

使用EXISTS对分组和costraint进行输出,其中addres对having子句是唯一的:

select *
from yourtable t1
where exists (
  select addressline
  from yourtable t2
  where t1.addressline = t2.addressline
  group by addressline
  having count(*) = 1
  )

答案 3 :(得分:0)

与COK(*)的VKP几乎相同的答案,但我更喜欢它作为公共表格表达式。

;WITH cte AS  ( 
    SELECT
       *
       ,COUNT(*) OVER (PARTITION BY AddressLine) as RecordsAtAddress
    FROM
       TableName
)

SELECT *
FROM
    cte
WHERE
    RecordsAtAddress = 1

但是,我确实想添加ROW_NUMBER窗口函数,以向您展示如何始终为每个AddressLine选择一条记录。因此,在这种情况下,您将获得1个重复记录,而不是将它们全部排除在一起。

;WITH cte AS (
    SELECT *
       ,ROW_NUMBER() OVER (PARTITION BY AddressLine ORDER BY CName) as RowNum
    FROM
       TableName
)

SELECT *
FROM
    cte
WHERE
    RowNum = 1

要谨慎使用IN或NOT IN特别是使用像这样的自由格式文本,因为如果AddressLine可以为NULL,那么你将得不到你所期望的!性能通常也不那么好。 NOT IN vs NOT EXISTS

对于EXISTS或NOT EXISTS答案,您实际上必须将您的子查询与此类关联,注意NOT EXISTS对于这样的案例可能表现更好:

SELECT *
FROM
    TableName t1
WHERE
    NOT EXISTS (SELECT
                t2.AddressLine
             FROM
                TableName t2
             WHERE
                t1.AddressLine = t2.AddressLine
             GROUP BY
                t2.AddressLine
             HAVING COUNT(*) > 1)