来自重复列的唯一行组合

时间:2019-07-12 14:17:47

标签: sql sql-server

我有一个包含两列的表。列A在列b中重复,列b是其他几张表的主键。我需要查找列A在列b中存在超过1次的位置。

我正在使用SQL2008

Table
Col A           Col B
0842311459      91
0884113434      91
9780842311458   91
9780884113430   91
9780842311458   95
9780884113430   95
0842359397      105
9780842359399   105
0687192706      135
9780687192700   135

我希望输出只是重复数据之一:

Col A         Col B
9780842311458   91
9780884113430   91
9780842311458   95
9780884113430   95

Col A           Col B
9780842311458   91 95
9780884113430   91 95

3 个答案:

答案 0 :(得分:1)

您可以使用EXISTS来做到这一点:

select t.cola, t.colb
from tablename t
where exists (
  select 1 from tablename
  where cola = t.cola and colb <> t.colb
)

对于第二版结果,请进行自我加入:

select t.cola, concat(t.colb, ' ', tt.colb) colb
from tablename t inner join tablename tt 
on tt.cola = t.cola and tt.colb > t.colb

请参见demo
结果:

> cola          | colb
> :------------ | :---
> 9780842311458 | 91  
> 9780884113430 | 91  
> 9780842311458 | 95  
> 9780884113430 | 95

> cola          | colb 
> :------------ | :----
> 9780842311458 | 91 95
> 9780884113430 | 91 95

答案 1 :(得分:0)

DECLARE
    @TestData TABLE
    (
        ColA VARCHAR(20),
        ColB INT
    );

INSERT INTO @TestData
VALUES
(
    '0842311459', 91
);
INSERT INTO @TestData
VALUES
(
    '0884113434', 91
);
INSERT INTO @TestData
VALUES
(
    '9780842311458', 91
);
INSERT INTO @TestData
VALUES
(
    '9780884113430 ', 91
);
INSERT INTO @TestData
VALUES
(
    '9780842311458', 95
);
INSERT INTO @TestData
VALUES
(
    '9780884113430', 95
);
INSERT INTO @TestData
VALUES
(
    '0842359397', 105
);
INSERT INTO @TestData
VALUES
(
    '9780842359399', 105
);
INSERT INTO @TestData
VALUES
(
    '0687192706', 135
);
INSERT INTO @TestData
VALUES
(
    '9780687192700', 135
);

SELECT
    *
FROM
    @TestData
WHERE
    ColA IN
    (
        SELECT
            ColA
        FROM
            @TestData
        GROUP BY
            ColA
        HAVING
            COUNT(*) > 1
    );

结果

9780842311458   91
9780884113430   91
9780842311458   95
9780884113430   95

Fiddle

答案 2 :(得分:0)

您可以使用cte获得相同的输出

    create table #Table
    ( 
        ColA    varchar(100),   
        ColB    int
    )

    insert into #Table values ('0842311459'      ,91)
    insert into #Table values ('0884113434'      ,91)
    insert into #Table values ('9780842311458'   ,91)
    insert into #Table values ('9780884113430'   ,91)
    insert into #Table values ('9780842311458'   ,95)
    insert into #Table values ('9780884113430'   ,95)
    insert into #Table values ('0842359397'      ,10)
    insert into #Table values ('9780842359399'   ,10)
    insert into #Table values ('0687192706'      ,13)
    insert into #Table values ('9780687192700'   ,13)

第一个查询

    ; with cte_first as (
    select  * 
    from    #Table
    )
    select  a.ColA, a.ColB
    from    cte_first a
    join    #Table b
            on a.ColA = b.ColA 
            and a.ColB <> b.ColB

第二次查询

    ; with cte_first as (
    select  * 
    from    #Table
    )
    select  a.ColA, concat(a.colb, ' ', b.colb) colb
    from    cte_first a
    join    #Table b
            on a.ColA = b.ColA 
            and a.ColB > b.ColB

第一个输出

    ColA            ColB
    --------------- ------
    9780842311458   95
    9780884113430   95
    9780842311458   91
    9780884113430   91

第二次输出

    ColA            colb
    --------------- ------
    9780842311458   95 91
    9780884113430   95 91