Select Rows with matching columns from SQL Server

前端 未结 3 956
一向
一向 2020-12-11 15:04

I am fairly certain that this is something simple, but every example I have tried is failing. I want to query a table like this

ID   Part_Type   Station_Typ         


        
相关标签:
3条回答
  • 2020-12-11 15:35
    select id, part_type, station_type 
    from myTable t1
    where exists (select 1 from myTable t2
                  where t1.part_type = t2.part_type
                      and t1.station_type = t2.station_type
                      and t1.id <> t2.id)
    
    0 讨论(0)
  • 2020-12-11 15:40

    I think a self-join will work for you:

    SELECT * FROM table t1 
    INNER JOIN table t2 ON t1.Part_Type = t2.Part_Type 
      AND t1.Station_Type = t2.Station_Type
      AND t1.Id <> t2.Id
    
    0 讨论(0)
  • 2020-12-11 15:41

    You can use the following:

    select t1.id, t1.part_type, t1.station_type
    from yourtable t1
    where exists (select part_type, station_type
                  from yourtable t2
                  where t1.part_type = t2.part_type
                    and t1.station_type = t2.station_type
                  group by part_type, station_type
                  having count(id) > 1)
    

    See SQL Fiddle with Demo

    0 讨论(0)
提交回复
热议问题