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)
 Get Boolean result

Author  Topic 

sql117
Starting Member

19 Posts

Posted - 2009-06-25 : 06:43:50
How can i get the result as boolean or(either 1 or 0) if the whole column contains same data or not? The data in table is as follows.

Id C2 C3
1 a b
1 a b
1 a b
2 g h
2 k h
3 p k
3 p k

If i query on this table for ID = 1 then i need to get boolean result whether Columns C2 & C3 contains same data or not.
For Example: if i query on Id = 1 then result is
Id C2 C3
1 a b
1 a b
1 a b
Here both columns contain same data. so the result is 1 or yes

For Example : if i query on Id = 2 then
Id C2 C3
2 g h
2 k h

Here Column C2 contains diff data. So the result is 0 or NO

How i get this through query.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-25 : 06:46:40
SELECT ID,
CASE WHEN MIN(c2) = MAX(c2) AND MIN(c3) = max(c3) then 1
else 0
end AS IsSame
FROM Table1
GROUP BY ID


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

sql117
Starting Member

19 Posts

Posted - 2009-06-25 : 07:15:51
Thank you. It works fine
quote:
Originally posted by Peso

SELECT ID,
CASE WHEN MIN(c2) = MAX(c2) AND MIN(c3) = max(c3) then 1
else 0
end AS IsSame
FROM Table1
GROUP BY ID


E 12°55'05.63"
N 56°04'39.26"


Go to Top of Page
   

- Advertisement -