【问题标题】:SQL SELECT Only 1 column when there is a duplicate in another column当另一列中有重复时,SQL SELECT 仅 1 列
【发布时间】:2020-06-29 18:08:46
【问题描述】:

首先让我说我只有我们数据库的读取权限。

我目前有多行具有相同值的列:

例子

Row | col1 | col2 | col3 | col4 | col5 |

1   | AA   | BB   | CC   | AA   | CC   |
2   | DD   | CC   | DD   | BB   | BB   |
3   | ZZ   | ZZ   | CC   | CC   | BB   |
4   | AA   | AA   | DD   | AA   | AA   |

我试图只选择跨行唯一的值

1| AA | BB | CC 
2| DD | CC | BB
3| ZZ | CC | BB 
4| AA | DD |   

我真的不知道从哪里开始。有点难倒这个。

【问题讨论】:

  • 您可能需要注意几个答案如何在脚本顶部复制您的数据 - 请注意它没有比您的表格大很多显示在您的问题中-但具有巨大的好处,它是可运行的代码,可让人们测试他们的脚本。我建议下次记笔记。

标签: sql sql-server select duplicates


【解决方案1】:

似乎您需要取消透视数据,获取DENSE_RANK,然后重新透视:

WITH YourTable AS(
    SELECT *
    FROM (VALUES(1,'AA','BB','CC','AA','CC'),
                (2,'DD','CC','DD','BB','BB'),
                (3,'ZZ','ZZ','CC','CC','BB'),
                (4,'AA','AA','DD','AA','AA'))V([Row],Col1,Col2,Col3,Col4,Col5)),
Unpvt AS(
    SELECT YT.[Row],
           V.Col,
           DENSE_RANK() OVER (PARTITION BY YT.[Row] ORDER BY V.Col) AS DR
    FROM YourTable YT
         CROSS APPLY (VALUES(YT.Col1),
                            (YT.Col2),
                            (YT.Col3),
                            (YT.Col4),
                            (YT.Col5))V(Col))
SELECT U.[Row],
       MAX(CASE U.DR WHEN 1 THEN U.Col END) AS Col1,
       MAX(CASE U.DR WHEN 2 THEN U.Col END) AS Col2,
       MAX(CASE U.DR WHEN 3 THEN U.Col END) AS Col3,
       MAX(CASE U.DR WHEN 4 THEN U.Col END) AS Col4,
       MAX(CASE U.DR WHEN 5 THEN U.Col END) AS Col5
FROM Unpvt U
GROUP BY U.[Row];

【讨论】:

    【解决方案2】:

    这有点棘手,但你可以使用apply 和条件聚合:

    select t.row, c.*
    from t cross apply
         (select max(case when seqnum = 1 then col end) as col1,
                 max(case when seqnum = 2 then col end) as col2,
                 max(case when seqnum = 3 then col end) as col3,
                 max(case when seqnum = 4 then col end) as col4,
                 max(case when seqnum = 5 then col end) as col5
          from (select col,
                       row_number() over (order by min(pos)) as seqnum
                from (values (1, t.col1), (2, t.col2), (3, t.col3), (4, t.col4), (5, t.col5))
                     ) v(pos, col)
                group by col
          ) c;
    

    【讨论】:

      【解决方案3】:

      这很不好看,因为有人将 SQL 表视为电子表格,就好像行和列完全一样。这是构建解决方案的一种方法:

      declare @t table (Row int, col1 char(2), col2 char(2), col3 char(2), col4 char(2), col5 char(2))
      insert into @t(Row,col1,col2,col3,col4,col5) values
      (1,'AA','BB','CC','AA','CC'),
      (2,'DD','CC','DD','BB','BB'),
      (3,'ZZ','ZZ','CC','CC','BB'),
      (4,'AA','AA','DD','AA','AA')
      
      select * from (
      select Row,Foo,'col' + CONVERT(varchar(10),ROW_NUMBER() OVER (PARTITION BY Row ORDER BY Bar)) as Bar2  from (
      select Row,Foo,Bar from (
      select
          *,ROW_NUMBER() OVER (PARTITION BY Row,Foo ORDER BY Bar) rn
      from @t
      unpivot (Foo for Bar in (col1,col2,col3,col4,col5)) u
      ) v
      where rn = 1
      ) w
      ) x
      pivot (MAX(Foo) for Bar2 in (col1,col2,col3,col4,col5)) y
      

      【讨论】:

        【解决方案4】:

        如果 2016+ 并且您有许多或可变的列

        示例

        Declare @YourTable Table ([Row] varchar(50),[col1] varchar(50),[col2] varchar(50),[col3] varchar(50),[col4] varchar(50),[col5] varchar(50))  Insert Into @YourTable Values 
         (1,'AA','BB','CC','AA','CC')
        ,(2,'DD','CC','DD','BB','BB')
        ,(3,'ZZ','ZZ','CC','CC','BB')
        ,(4,'AA','AA','DD','AA','AA')
        
        Select *
         From  (
                Select A.[Row]
                      ,B.* 
                 From  @YourTable A
                 Cross Apply (
                                Select [Value] 
                                      ,ColNr = concat('Col',dense_rank() over (order by value))
                                 From  OpenJson( (Select A.* For JSON Path,Without_Array_Wrapper ) ) 
                                 Where [Key] not in ('Row')
                             ) B
              ) src
         Pivot (max(Value) for ColNr in ([Col1],[Col2],[Col3],[Col4],[Col5]) ) pvt
        

        退货

        Row Col1    Col2    Col3    Col4    Col5
        1   AA      BB      CC      NULL    NULL
        2   BB      CC      DD      NULL    NULL
        3   BB      CC      ZZ      NULL    NULL
        4   AA      DD      NULL    NULL    NULL
        

        【讨论】:

          猜你喜欢
          • 2019-01-19
          • 1970-01-01
          • 2017-12-29
          • 2015-01-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-31
          • 1970-01-01
          相关资源
          最近更新 更多