views:

87

answers:

5

How to know if all the cells have the same value in some column (title changed)

I want to have a bit scalar value that tells me if all the values in a column equal something:

DECLARE @bit bit
SELECT @bit = TRUEFORALL(Name IS NOT NULL) FROM Contact

UPDATE

I now realized that I actually don't need the TrueForAll, what I do need is to make sure, that all values in a column are equal, for example, I want to know whether all Group.Items have the same price.

+1  A: 

Maybe this?

DECLARE @bit bit
if exists(SELECT Name FROM Contact WHERE Name IS NULL) 
   SET @bit = 0
ELSE
  SET @bit = 1
eKek0
Holy @@@@ that was so quick!
Shimmy
Of course, this is the opposite of the logic expressed in the OP's question. It should be `WHERE Name IS NULL` and set the `bit` to 0, otherwise set it to 1.
Adam Robinson
Exists will return true if 1+ records satisfy the logic
OMG Ponies
I made a mistake in my question, please review it. thanks and sorry.
Shimmy
A: 

This solves your first question:

SELECT
    CASE
        WHEN EXISTS(
            SELECT 1
            FROM Contact
            WHERE Name IS NULL
        ) THEN 0
        ELSE 1
    END

ADDED:

This will solve your second:

SELECT
    CASE
        WHEN EXISTS(
            SELECT TOP 1 1 FROM (
                SELECT
                    ItemGroupName,
                    COUNT(Price) AS CNT
                FROM ItemGroup
                GROUP BY ItemGroupName
                HAVING COUNT(Price) > 1
            ) t
        ) THEN 0
        ELSE 1
    END

By the way, when you use the exists function, its better to SELECT 1 (a constant) so less data gets returned

Gabriel McAdams
+1  A: 

For your updated requirement something like this would appear to do what you want:

DECLARE @IsSameGroup bit
SELECT @IsSameGroup = CASE WHEN COUNT(*) > 1 THEN 0 ELSE 1 END
FROM (SELECT Name FROM Contact GROUP BY Name) groups

When the count is greater the 1 you have two different names (or prices depending on what you group on)

David Hall
Or just `select count(name) from Contact`, right?
Blorgbeard
@Blorgbeard The subselect gives the count of the number of groups. Without that you just get multiple records (one per group). Horses for courses really.
David Hall
A: 

Not very good for NULLs, but 2008 can do:

SELECT 1 WHERE 'Blue' = ALL ( SELECT Color FROM dbo.Hat )

OR

DECLARE @bit bit

SET @bit = 
CASE ( SELECT 1 WHERE 'Blue' = ALL ( SELECT Color FROM dbo.Hat ))
WHEN 1 THEN 1 ELSE 0 END

UPDATE

All same color

SET @bit = 
CASE(
   SELECT 1 WHERE
  (SELECT TOP(1) Color FROM dbo.Hat) = ALL ( SELECT Color FROM dbo.Hat )
    )
WHEN 1 THEN 1 ELSE 0 END
Damir Sudarevic
I am using 2005 but good to know, thanks for posting.
Shimmy
+2  A: 

Why not?

select count( distinct price) from table

If returns 1, all values are the same... Add

where price is not null

if need be

Sparky
upvoted for count(distinct x), which seems to be the most obvious solution here to me (certainly the first one I thought of) and is also valid ANSI SQL 92, and so as cross-playform as you'll get.
Cowan