查找重复并为SQL中的数据提供密钥标识符

时间:2016-05-26 11:33:21

标签: sql sql-server

我有一个简单的表格:

enter image description here

下面给出的脚本

    CREATE TABLE SimpleTable
(
    Name varchar(100),
    Postcode  varchar(100),
    VATNumber Varchar(100),
    SANNumber varchar(100)
)

INSERT INTO dbo.SimpleTable
(
    Name,
    Postcode,
    VATNumber,
    SANNumber
)
VALUES
(
    'UMESH', -- Name - varchar
    '431001', -- Postcode - varchar
    '4567', -- VATNumber - Varchar
    '9090' -- SANNumber - varchar
)
INSERT INTO dbo.SimpleTable
(
    Name,
    Postcode,
    VATNumber,
    SANNumber
)
VALUES
(
    'UMESH', -- Name - varchar
    '431001', -- Postcode - varchar
    '9094', -- VATNumber - Varchar
    '9090' -- SANNumber - varchar
)
INSERT INTO dbo.SimpleTable
(
    Name,
    Postcode,
    VATNumber,
    SANNumber
)
VALUES
(
    'Prajakta', -- Name - varchar
    '431001', -- Postcode - varchar
    '9094', -- VATNumber - Varchar
    '5555' -- SANNumber - varchar
)

INSERT INTO dbo.SimpleTable
(
    Name,
    Postcode,
    VATNumber,
    SANNumber
)
VALUES
(
    'Prajakta', -- Name - varchar
    '777777', -- Postcode - varchar
    '9094', -- VATNumber - Varchar
    '5555' -- SANNumber - varchar
)

INSERT INTO dbo.SimpleTable
(
    Name,
    Postcode,
    VATNumber,
    SANNumber
)
VALUES
(
    'Prajakta', -- Name - varchar
    '777777', -- Postcode - varchar
    '9094', -- VATNumber - Varchar
    '5555' -- SANNumber - varchar
)
INSERT INTO dbo.SimpleTable
(
    Name,
    Postcode,
    VATNumber,
    SANNumber
)
VALUES
(
    'Ramesh', -- Name - varchar
    '12345', -- Postcode - varchar
    '9089', -- VATNumber - Varchar
    '4534' -- SANNumber - varchar
)

我想要的输出是什么 如果在名称,邮政编码,增值税号或SAN号等任何单一匹配上,那么我想为其分配一个密钥标识符。 到目前为止,我已尝试过以下查询

SELECT ROW_NUMBER() OVER (order by NAME) AS KeyIdentifier,st.Name,st.Postcode,st.VATNumber,st.SANNumber
FROM dbo.SimpleTable st
GROUP BY NAME,st.Postcode,st.VATNumber,st.SANNumber
HAVING COUNT(NAME)>1 AND COUNt(st.Postcode)>1 AND Count(st.VATNumber)>1 AND Count(st.SANNumber)>1

但是这给了我一个带有一个标识符的记录 但我想要的是这样的

一般情况下,如果在名称,邮政编码,增值税号或SAN号码上有任何匹配,则为该记录分配密钥标识符。如果您看到结果密钥标识符3已分配,因为增值税是这样匹配的。 任何帮助表示赞赏。

enter code here

enter image description here

3 个答案:

答案 0 :(得分:0)

SELECT DISTINCT
    id,
    single.name,
    Postcode,
    VATNumber,
    SANNumber

FROM
(SELECT ROW_NUMBER() over (order by name) as id ,
        name
 FROM 
     (SELECT distinct name 
      FROM SimpleTable) as names
 ) as single INNER JOIN
(SELECT PostCode,
        name,
        VATNumber,
        SANNumber 
 FROM SimpleTable) as nfo
ON single.name = nfo.Name

尝试这可能是你想要的。

http://sqlfiddle.com/#!6/15ae2/1/0

答案 1 :(得分:0)

您的要求并不完全合理,我不确切知道您将对结果做些什么...此查询会对四个字段中的密钥标识符进行分组,希望这可以帮助您入门。

;
With cteAssign As (
Select  Dense_Rank() OVER (Order By Name) AS KeyIdentifierName,
        Row_Number() OVER (Partition By Name Order By Name) AS KeyOrderName,
        Dense_Rank() OVER (Order By PostCode) AS KeyIdentifierPostCode,
        Row_Number() OVER (Partition By PostCode Order By PostCode) AS KeyOrderPostCode,
        Dense_Rank() OVER (Order By VatNumber) AS KeyIdentifierVatNumber,
        Row_Number() OVER (Partition By VatNumber Order By VatNumber) AS KeyOrderVatNumber,
        Dense_Rank() OVER (Order By SanNumber) AS KeyIdentifierSanNumber,
        Row_Number() OVER (Partition By SanNumber Order By SanNumber) AS KeyOrderSanNumber,
        st.Name,st.Postcode,st.VATNumber,st.SANNumber
    From dbo.SimpleTable st
)
Select * From cteAssign Order By KeyIdentifierName  -- By Name
--Select * From cteAssign Order By KeyIdentifierPostCode    -- By PostCode
--Select * From cteAssign Order By KeyIdentifierVatNumber   -- By VatNumber
--Select * From cteAssign Order By KeyIdentifierSanNumber   -- By SanNumber

答案 2 :(得分:0)

也许:

create table #SimpleTable(
    name varchar(50),
    Postcode varchar(50),
    VATNumber varchar(50),
    SANNumber varchar(50),
)

insert into #SimpleTable values( 'MAHESH', '101114', '4567', '9090' )
insert into #SimpleTable values( 'MAHESH', '101114', '9094', '9090' )
insert into #SimpleTable values( 'pajacta', '101114', '9094', '5555' )
insert into #SimpleTable values( 'pajacta', '7777', '9094', '5555' )
insert into #SimpleTable values( 'rammer', '12121', '9089', '4534' )
insert into #SimpleTable values( 'pajacta', '7777', '9094', '5555' )

select * from #SimpleTable

MAHESH  101114  4567    9090
MAHESH  101114  9094    9090
pajacta 101114  9094    5555
pajacta 7777    9094    5555
rammer  12121   9089    4534
pajacta 7777    9094    5555

;with cte as(
    select
        a.name  
    from
        #SimpleTable  a
    group by
        a.name
    having
        (count(distinct a.Postcode) > 1) 
        or (count(distinct a.VATNumber) > 1) 
        or (count(distinct a.SANNumber) > 1)
),
cte1 as(
    select distinct * from #SimpleTable
)
select
    KeyIdentifier = row_number() over(partition by a.name order by a.name ),
    a.name, 
    a.Postcode, 
    a.VATNumber,
    a.SANNumber 
from
    cte1  a
    left join cte on (a.name = cte.name)

1   MAHESH  101114  4567    9090
2   MAHESH  101114  9094    9090
1   pajacta 101114  9094    5555
2   pajacta 7777    9094    5555
1   rammer  12121   9089    4534


drop table #SimpleTable