I have an intermediate table called ratingsPerId that is produced from a stored procedure shown here:
Id Rating
'1', 'low'
'1', 'low'
'1', 'low'
'1', 'low'
'1', 'low'
'2', 'medium'
'2', 'medium'
'2', 'medium'
'2', 'high'
'2', 'high'
'4', 'high'
'4', 'high'
'4', 'high'
'4', 'high'
'4', 'high'
'9', 'high'
'9', 'high'
'9', 'high'
'9', 'high'
'9', 'high'
'9', 'high'
'9', 'high'
'9', 'high'
'9', 'high'
What I want is for Id 1, count up all the lows, mediums, highs. And do the same for all the following Id 2, 4, 9, etc...
Right now what I have is a table join that counts the Highs and Lows per ID:
drop temporary table if exists t1, t2;
create temporary table t1(ruleid int, high INT);
INSERT into t1(
SELECT ruleid, count(*) AS High from ratingsPerRuleId WHERE rating='high' group by ruleid);
create temporary table t2(ruleid int, low int);
INSERT into t2(
SELECT ruleid, count(*) AS Low from ratingsPerRuleId WHERE rating='low' group by ruleid);
SELECT t1.RuleID, t1.high, t2.low from t1 left join t2 on t1.ruleid=t2.ruleid ;
ID High Low
1 NULL 5
2 2 NULL
4 5 NULL
9 9 NULL
As you can see, I will need to create a third table for Medium and do a join of that to get the Medium column.
Is there an easier way to do this in MYSQL? I feel like there is a simple statement that can accomplish this, and my messy table joins of small tables aren't necessary but I don't know enough of MYSQL to get the answer.
I looked into Count(distinct) and Select Distinct but haven't found the answer.
Thanks!