Sql Multiple Count From Single Table By Different Values of same Column

Sql Multiple Count From Single Table By Different Values of same Column

 


Query : - select sum(case when rating = '1' then 1 else 0 end) rating1,
sum(case when rating = '2' then 1 else 0 end) rating2,
sum(case when rating = '3' then 1 else 0 end) rating3,
sum(case when rating = '4' then 1 else 0 end) rating4,
sum(case when rating = '5' then 1 else 0 end) rating5
from kit_rating .


Where rating is your column and 1,2,3,4,5 is your type of value.
And kit_rating is your table name.

Now. Study More Deep What this actually Do.

SUM : Sum is Generally used to sum up the values in colums.
Here We used sum As increment since this query fetch the rating where value is 1 if value is
1 if it match then it increase the value in rating1 field. If it fails then it add zero to the field value.it is a good way to count the number of value by using condition for programmers.
If it match then in increase 1.
else increase 0.

We already use this technique in our various Programs.
Previous Post
Next Post

post written by:

0 comments: