SimanisJKT48
SimanisJKT48

Reputation: 104

SQL server count another rows

i have database like this...

how i can result like this ?

how i can result like this ? in my mind i can query = >

select id, count(no1, no2, no3) where no1='B',no2='B',no3='B'

thank's very much.

Upvotes: 0

Views: 34

Answers (2)

Rigel1121
Rigel1121

Reputation: 2016

Use Case When statements with Count aggregate. Lastly, Group them with id:

Select id, 
       count(case when  no1='B' then 1 END) +
       count(case when  no2='B' then 1 END) +
       count(case when  no3='B' then 1 END) AS count_all
From yourtable
Group by id

Upvotes: 1

Pரதீப்
Pரதீப்

Reputation: 93694

Use Case Statement

select id, 
       case when  no1='B' then 1 else 0 END +
       case when  no2='B' then 1 else 0 END +
       case when  no3='B' then 1 else 0 END As Count_All
From yourtable

Upvotes: 3

Related Questions