Guntis Endzelis
Guntis Endzelis

Reputation: 75

MYSQL query to get percentage of each rating

Is there a simple query to achieve the following?

I have the following table: ID RATING 1 1 2 2 3 5 4 5

I need to get the percentages of each rating like this 1 25 2 25 5 50

Upvotes: 1

Views: 371

Answers (1)

Abhilash Ravindran C K
Abhilash Ravindran C K

Reputation: 1856

Following code will be helpful to you,

SELECT RATING, (COUNT(*) / (SELECT COUNT(*) FROM your_table)) * 100 AS Percentage
FROM your_table
GROUP BY RATING;

SQL Fiddle Demo

Upvotes: 4

Related Questions