Count number of rows with specific values - mysql

I'm storing ratings for an item in a table called ratings.
value is an integer between 0 and 7 (the rating value).
As an example, let's say showcase_id = 1 has 10 total ratings:
5 ratings are value = 7
2 ratings are value = 6
2 ratings are value = 5
1 rating is value = 4
no ratings for values 0,1,2,3
Is there any efficient way I can select the total number of ratings for each specific value, the total number of ratings and the average rating from 1 single query?
e.g. the number of rows/count WHERE value = 6 is 2. Would I need to do 7 separate subqueries?
SELECT AVG(value),COUNT(*),????
FROM ratings
WHERE showcase_id = :showcase_id

Are you referring to count(distinct)?
SELECT AVG(value), COUNT(*), COUNT(DISTINCT value)
FROM ratings
WHERE showcase_id = :showcase_id;
EDIT:
If you want the total for each value, you can stuff this into one column, using a subquery:
SELECT SUM(cnt * value) / SUM(cnt) as average,
SUM(cnt) as total_cnt,
GROUP_CONCAT(value, '-', cnt ORDER BY VALUE) as values
FROM (SELECT value, COUNT(*) as cnt
FROM ratings r
WHERE showcase_id = :showcase_id
GROUP BY value
) r;
Perhaps the subquery meets your needs as well.

You can use the WITH ROLLUP modifier to GROUP BY to get the counts and average for each value, as well as the totals for everything (if you group by multiple columns it will also create subtotals for each inner group).
SELECT value, AVG(value) AS avg, COUNT(*) as count
FROM ratings
WHERE showcase_id = 1
GROUP BY value WITH ROLLUP
This will produce a row for each value with its count, and then a row with value = NULL for that aggregates the entire result.
DEMO

What's wrong with group by?
SELECT AVG(value),COUNT(*), value
FROM ratings
WHERE showcase_id = :showcase_id
GROUP BY value;
EDIT (with the overall avg & totes):
select count(*) total_by_value, value, s1.full_avg, s1.full_total
from ratings r,
(select avg(value) full_avg, count(*) full_total from ratings) s1
group by s1.full_avg, s1.full_total, value;

Related

Mysql calculate with the max() value

I'm a bit stuck cause am not sure if I am looking the wrong way casue I can't find anything related to what I am looking for. So I want to give an example for what I am trying to do. NOTE: not real scenario.
I have a count on a field:
SELECT count(user_id)
FROM usersgroups
group by user_id
Now I want to make a calc with the max value from the count. Something like:
SELECT count(user_id) as count,
SUM(max(count(user_id)) / count(user_id)) as blub
FROM usergroups
group by user_id
But I get error invalid use of group function cause I think I cant use the COUNT() function inside the SUM function?
So is there any other way to make that calculation.
P.S. the calc is for calculating the percentage of the max value to the record/current value.
UPDATE
So what I expect is a percentage like
count | percentage
5 | 1
4 | 0.8
3 | 0.6
2 | 0.3
1 | 0.2
5/5 = 1
4/5 = 0.8
3/5 = 0.6
2/5 = 0.4
1/5 = 0.2
In a separate Derived Table, you can fetch the maximum count value out of all the counts. I have simply used ORDER BY COUNT(user_id) DESC LIMIT 1 to fetch the same.
CROSS JOIN this result-set with the usergroups table, so that every row in the usergroups table has access to the maximum count value.
Now, you can simply use the GROUP BY and appropriate aggregation to determine the "percent".
Note that, for GROUP BY to be valid, SELECT clause must contain either aggregated columns/expressions only, or the columns specified in the GROUP BY clause. That is why MAX() is used over the maximum count value. Since that value is only a scalar, so MAX() will return the same value only.
Try the following:
SELECT
ug.user_id,
COUNT(ug.user_id) AS user_count,
COUNT(ug.user_id) / MAX(mcnt.count) AS percent
FROM
usergroups AS ug
CROSS JOIN
(
SELECT COUNT(user_id) AS count
FROM usersgroups
GROUP BY user_id
ORDER BY COUNT(user_id) DESC LIMIT 1
) AS mcnt
GROUP BY ug.user_id
ORDER BY user_count DESC
Count available user_id as subquery, then divide user_id values by the sum of count.
select user_id/ sum(count) as percentage
from(
select count(user_id) as count , user_id
from usergroups
where user_id != 0
group by user_id
) as A
group by count, user_id

mysql join two rows with same column value and calculate certain column values and return all rows in one row

I have a table name agents_commission have columns id, agent_id, deal_id, commission, deal_date.
I want to select agent_id, commission from agents_commission where month(date_deal) = '$month' and year(date_deal) ='$year';
but if the table have some rows have the same agent_id join the same rows in one row and calculate the commission values like row 66666.7 + 100000 as in row 1, 2
to be the final fetch_assoc as
array('agent_id'=>1, 'commision'=>166666.7);
thanks
You can not select all when you want to group by only one column(agentid). you can do something like below
select agent_id, sum(commission) as commission from temp where MONTH(deal_date) = '09' and year(deal_date) ='2018' group by agent_id;
The SELECT statement used in the GROUP BY clause can only be used contain column names, aggregate functions, constants and expressions.
Refer https://dev.mysql.com/doc/refman/5.5/en/group-by-modifiers.html
Try using group by and aggregation:
select agentid, sum(commission) as commission
from agents_commission
where month(date_deal) = '$month' and year(date_deal) ='$year';
group by agentid

Progression Series generation so that Show n candidates having highest score and n+5 candidates having second highest score and so on

have a table which have 3 columns. Id is unique and score can be duplicate in that table and table have more than thousand of entries in it.
Id Name Score
Problem statement:-
I have to show list in such manner such that
List of 5:candidate of having highest score then List of 10: candidate of having second highest score then List of 15: candidate of having third highest score
and so on..(Each incremented by 5)
select #mem_count:=#mem_count+5 as group_member_count, scores.score
from (select distinct score from scores_table order by score desc) scores,
(select #mem_count:=0) sess
The query returns list of scores plus member count for each score. That would be subquery to be used in the next statement.
select
sc.*,
#group_n:=if(sc.score!=#group,0,#group_n+1) number_in_group,
#group:=sc.score
from
(SELECT #group_n:=0, #group:=-1) row,
scores_table sc
join (the_subquery) sub on sc.score=sub.score
having number_in_group<sub.group_member_count
Here we define 2 session variables group (to keep current score) and group_n to add numbers in group for each row.
Thus we expect to kind fo group by score and number all rows for each score. Then the having willleave only members less than group_member_count
Use this
SELECT x.*
FROM my_table x
JOIN my_table y
ON y.marks = x.marks
AND y.id <= x.id
GROUP
BY x.marks
, x.id
HAVING COUNT(0) <= (MAX(101-x.marks)*2)
ORDER
BY marks DESC,id;

MySQL select group by having column != x

I'm trying to select rows where a certain column does not have a certain value, such as 0, but I'm unable to get this to work.
SELECT *
FROM rentals
GROUP BY date, rooms, price
HAVING show_independently < 1
If show_independently is 1, then I don't want to group them. However, this statement shows no rows even though most rows have show_independently as 0.
SELECT date, rooms, price
FROM rentals
WHERE show_independently < 1
GROUP BY date, rooms, price
If you only want to group some rows and leave others ungrouped, you can use a UNION:
SELECT *
FROM rentals
WHERE show_independently <> 1
GROUP BY date, rooms, price
UNION ALL
SELECT *
FROM rentals
WHERE show_independently = 1
This groups only those where show_independently is not 1, and includes the rest without grouping them.
A HAVING clause is used when you are using an aggregate function to filter data.
A typical query with HAVING:
SELECT yourColumn, aggregate_function(otherColumn)
FROM yourTable
WHERE yourColumn = someValue
GROUP BY yourColumn
HAVING aggregate_function(otherColumn) = someOtherValue
I think you want to be using a WHERE clause:
SELECT date, rooms, price
FROM rentals
WHERE show_independently < 1
GROUP BY date, rooms, price

SELECT rows with minimum count(*)

Let's say i have a simple table voting with columns
id(primaryKey),token(int),candidate(int),rank(int).
I want to extract all rows having specific rank,grouped by candidate and most importantly only with minimum count(*).
So far i have reached
SELECT candidate, count( * ) AS count
FROM voting
WHERE rank =1
AND candidate <200
GROUP BY candidate
HAVING count = min( count )
But,it is returning empty set.If i replace min(count) with actual minimum value it works properly.
I have also tried
SELECT candidate,min(count)
FROM (SELECT candidate,count(*) AS count
FROM voting
where rank = 1
AND candidate < 200
group by candidate
order by count(*)
) AS temp
But this resulted in only 1 row,I have 3 rows with same min count but with different candidates.I want all these 3 rows.
Can anyone help me.The no.of rows with same minimum count(*) value will also help.
Sample is quite a big,so i am showing some dummy values
1 $sampleToken1 101 1
2 $sampleToken2 102 1
3 $sampleToken3 103 1
4 $sampleToken4 102 1
Here ,when grouped according to candidate there are 3 rows combining with count( * ) results
candidate count( * )
101 1
103 1
102 2
I want the top 2 rows to be showed i.e with count(*) = 1 or whatever is the minimum
Try to use this script as pattern -
-- find minimum count
SELECT MIN(cnt) INTO #min FROM (SELECT COUNT(*) cnt FROM voting GROUP BY candidate) t;
-- show records with minimum count
SELECT * FROM voting t1
JOIN (SELECT id FROM voting GROUP BY candidate HAVING COUNT(*) = #min) t2
ON t1.candidate = t2.candidate;
Remove your HAVING keyword completely, it is not correctly written.
and add SUB SELECT into the where clause to fit that criteria.
(ie. select cand, count(*) as count from voting where rank = 1 and count = (select ..... )
The HAVING keyword can not use the MIN function in the way you are trying. Replace the MIN function with an absolute value such as HAVING count > 10