Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Sets Equivalent

Author  Topic 

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-10-04 : 19:55:08
Hi all,

Suppose A, B are Sets (table):
A = {1, 2, 3}
B = (1, 2, 3}
I want when A = B then Select 1 else Select 0
And I know following method:
Where Count (A) = Count (B) AND Count (A intersect B) = Count (A)

I am looking for others and best methods!


Set NoCount On
Declare @A Table (i int)
Declare @B Table (j int)

Insert into @A Values (1)
Insert into @A Values (2)
Insert into @A Values (3)

Insert into @B Values (1)
Insert into @B Values (2)
Insert into @B Values (3)


select case when exists
(
select *
where
(select COUNT(*) from(
Select * FROM @A
INTERSECT
Select * FROM @B )d )= (select COUNT(*) from @A)

and (select COUNT(*) from @A)=(select COUNT(*) from @B)
) then 1 else 0 end

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-10-04 : 20:13:22
I know another method.
Please post other suggestions and solutions

--case when (B-A)=Null And (A-B)=Null then 1 else 0

select case when not exists (select * from @A
except
select * from @B) and
not exists (select * from @B
except
select * from @A) then 1 else 0 end

Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2009-10-05 : 10:01:51
I am waiting still!

Perhaps, semi-join method is best method. But I do not know.

SELECT CASE WHEN EXISTS
(
SELECT
1
FROM
(SELECT k=1, * FROM @A) a
LEFT OUTER JOIN
(SELECT k=1, * FROM @B) b
ON a.i=b.j
GROUP BY
a.k
HAVING
COUNT(a.i)=(SELECT COUNT(*) FROM @B)
AND
COUNT(a.i)=COUNT(b.j)
) THEN 1 ELSE 0 END
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2009-10-05 : 10:44:40
quote:
Originally posted by ms65g

I am waiting still!



...so are we





Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -