Abhi
Abhi

Reputation: 1

SQL Count Query with GroupBy

I have data in table in the below format

ID CSLang VBLang Level Occurrence
1  FALSE  TRUE   1 - 4  1
2  FALSE  TRUE   5 - 9  1
3  FALSE  TRUE   0 - 0  1
4  TRUE   FALSE  1 - 4  1
5  TRUE   FALSE  5 - 9  1
6  TRUE   FALSE  10 - 15 1
7  TRUE   TRUE   0 - 0  1
8  FALSE  FALSE  0 - 0  1


I would like to get data as below (I want count of CSharp and VB which are true grouped by Level)

Level CountCS CountVB       
0 - 0   1       2       
1 - 4   1       1       
5 - 9   1       1       
10 - 15 1       0

Upvotes: 0

Views: 65

Answers (1)

Rahul
Rahul

Reputation: 77876

You can try like below (Assuming that you are using MySQL). See a demo fiddle http://sqlfiddle.com/#!2/08b00f/4

select level,
sum(CSharpLang = 'true') as CountCSharp,
sum(VBLang = 'true') as CountVBLang     
from table1
group by level
order by level

Per Update, if you are using SQL Server then you can do like below. A demo fiddle http://sqlfiddle.com/#!3/7fa05/3

select level,
sum(case when CSLang = 'true' then 1 else 0 end) as CountCSharp,
sum(case when VBLang = 'true' then 1 else 0 end) as CountVBLang     
from table1
group by level
order by level

Upvotes: 2

Related Questions