user7194270 user7194270 - 6 months ago 32
SQL Question

How to compare number of rows with no of column with value in SQL

I want to know if we can compare no.of row count and a column with value


id flag1 flag2 flag3
1 Y Y Y
2 N Y Y
3 Y Y N

count(*) = 3
and if
count(column) with 'Y'
matches with
then it should return



Thanks in advance


Your question is a little unclear. One interpretation is that you want to know if all the values in a column are "Y". You can do this with aggregation and a case:

select (case when min(flag1) = max(flag1) and min(flag1) = 'Y' then 'True' else 'False' end) as flag1_ally,
       (case when min(flag2) = max(flag2) and min(flag2) = 'Y' then 'True' else 'False' end) as flag2_ally,
       . . .
from t;