Home > Blockchain >  Get count of lDs where all status is greater than equal to 1
Get count of lDs where all status is greater than equal to 1

Time:10-15

I want to select count of ids where all status is greater than equal to 1

I want something like this

SELECT count(ID)FROM  table where all status >= 1
ID status
1 1
1 2
1 1
1 1
1 1
1 1
2 0
2 1
2 0
2 1
1 1
3 1
3 1
3 1
3 2
3 2
3 2

As you can see in the table where ID = 1,3 has all status greater than equal to 1.
I want to select the count of those IDs.
For example, I want count 2 from the above table.
How can I get those ids count where all status is >=1

Edited:Edited table

CodePudding user response:

To get the ID where all status are >=1 you can use

SELECT ID
FROM   Table1
GROUP  BY ID
HAVING MIN([status]) >= 1 

to count them you can use

SELECT COUNT(*)
FROM   (SELECT ID
        FROM   Table1
        GROUP  BY ID
        HAVING MIN([status]) >= 1) T 
  • Related