8.3.08

My T-SQL works

Hi Friends,
I got a peculiar problem from MSDN users and try to resolve the same.
OOPS its takes more than 15 minutes to arrive the solution. I came with three solutions but two solutions were previously given by others. This is my solution for the problem.

Problem Faced

GID Value
------------------------------------
1 True
1 True
2 False
2 True
3 True

If any GID with false should return the result set as False. GID with only true should return "True"

Code Snippet

DECLARE @t TABLE (GID int, Value varchar(10))

INSERT @t (GID, Value) VALUES (1, 'True')
INSERT @t (GID, Value) VALUES (1, 'True')
INSERT @t (GID, Value) VALUES (1, 'True')
INSERT @t (GID, Value) VALUES (2, 'False')
INSERT @t (GID, Value) VALUES (2, 'True')
INSERT @t (GID, Value) VALUES (3, 'True')
INSERT @t (GID, Value) VALUES (4, 'False')
INSERT @t (GID, Value) VALUES (5, 'True')

(select distinct t1.GID,t1.Value from @t as t1 inner join @t as t2 on t1.GID=t2.GID
where t1.gid not in (select t3.gid from @t as t3 where t3.value ='false'))
union
(select distinct t3.GID,t3.Value from @t as t3 where t3.value ='false')
select * from @t

No comments:

Post a Comment