Find groups with matching rows

前端 未结 5 588
庸人自扰
庸人自扰 2020-12-22 01:11

I have a table of people (CarOwners) and the types of cars they own

+-------+-------+
| Name  | Model |
+-------+-------+
| Bob   | Camry |
| Bo         


        
5条回答
  •  醉话见心
    2020-12-22 01:48

    Try it's

    if object_id('tempdb.dbo.#temp') is not null
    drop table #temp
    
    create table #temp (name varchar(100),model varchar(100))
    
    insert into #temp values('Bob','Camry')
    insert into #temp values('Bob','Civic')
    insert into #temp values('Bob','Prius')
    insert into #temp values('Kevin','Focus')
    insert into #temp values('Kevin','Civic')
    insert into #temp values('Mark','Civic')
    insert into #temp values('Lisa','Focus')
    insert into #temp values('Lisa','Civic')
    
    select * from (
    select row_number() over(partition by name order by (select null)) as n,
    row_number() over(partition by model order by (select null)) as m,*
    from #temp) as a
    where  n = m
    order by name
    

提交回复
热议问题