TSQL Comparing two Sets

后端 未结 6 2032
野的像风
野的像风 2021-01-18 03:58

When two sets are given

s1 ={ a,b,c,d} s2={b,c,d,a}

(i.e)

TableA

Item
a
b
c
d

TableB

Item
b
c
d
a

How to write Sql quer

6条回答
  •  感情败类
    2021-01-18 04:45

    Watch out, I'm gonna use a Cross Join.

    Declare @t1 table(val varchar(20))
    Declare @t2 table(val varchar(20))
    
    
    insert into @t1 values ('a')
    insert into @t1 values ('b')
    insert into @t1 values ('c')
    insert into @t1 values ('d')
    
    
    insert into @t2 values ('c')
    insert into @t2 values ('d')
    insert into @t2 values ('b')
    insert into @t2 values ('a')
    
    select 
        case when 
        count(1) = 
        (((Select count(1) from @t1) 
        + (Select count(1) from @t2)) / 2.0) 
        then 1 else 0 end as SetsMatch  from 
    @t1 t1 cross join @t2 t2 
    where t1.val = t2.val
    

提交回复
热议问题