开发者社区> 问答> 正文

SQL Server:如何查找重复的行值

SQL Server:如何查找重复的行值

展开
收起
贺贺_ 2019-12-02 19:35:41 369 0
1 条回答
写回答
取消 提交回答
  • 您可以尝试以下解决方案:

    ;with temp_data as ( 
    select 1 as num1 , 1 as num2 , 1 as num3 , 2 as num4 , 3 as num5 , 4 as num6 union all 
    select 1 as num1 , 1 as num2 , 1 as num3 , 1 as num4 , 3 as num5 , 4 as num6 union all 
    select 2 as num1 , 2 as num2 , 2 as num3 , 3 as num4 , 2 as num5 , 6 as num6 
    ) 
    SELECT * 
    FROM temp_data
    where 3=(CASE WHEN num1=num2 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num1=num3 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num1=num4 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num1=num5 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num1=num6 THEN 1 ELSE 0 END) 
    OR 
    3=(CASE WHEN num2=num1 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num2=num3 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num2=num4 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num2=num5 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num2=num6 THEN 1 ELSE 0 END) 
    OR 
    3=(CASE WHEN num3=num1 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num3=num2 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num3=num4 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num3=num5 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num3=num6 THEN 1 ELSE 0 END) 
    OR 
    3=(CASE WHEN num4=num1 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num4=num3 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num4=num2 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num4=num5 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num4=num6 THEN 1 ELSE 0 END) OR 
    3=(CASE WHEN num5=num1 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num5=num2 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num5=num4 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num5=num3 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num5=num6 THEN 1 ELSE 0 END) 
    OR 
    3=(CASE WHEN num6=num1 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num6=num2 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num6=num4 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num6=num3 THEN 1 ELSE 0 END)+ 
    (CASE WHEN num6=num5 THEN 1 ELSE 0 END) 
    ;
    
    

    输出:

    num1 num2 num3 num4 num5 num6 
    1      1 	1 	 1     3    4 
    2      2     2    3    2     6
    
    2019-12-02 19:36:31
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
SQL Server 2017 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载