How to get counts of different values on the same line? - mysql

I need a report with the count of the different values a column has, grouped by date, one output line per date.
The table columns are date, location ID (var) and rating (var). Rating can have only one of four values, ‘A’, ‘B’, ‘C’ or null/blank. I can get one line per date per value with this select, but how can I get the counts for all four values on the same line, for each date?
SELECT date, rating, count(rating) AS ratings
FROM table
GROUP BY date, rating

Use SUM with a boolean as follows:
SELECT
date,
SUM(rating = 'A') AS count_A,
SUM(rating = 'B') AS count_B,
SUM(rating = 'C') AS count_C,
SUM(rating = '' OR rating IS NULL) AS count_blank
FROM yourtable
GROUP BY date

Related

how to sum a list of max values group by

I'm trying to sum the max values of a column named value1 grouped by sensor id value so I want the last greater value from each sensor id.
Using this code I get a column with rows with the max results for each sensor. But I dont know how to sum this values.
select max(value1) from `digiwork_esp-data`.SensorData group by sensor
I need to sum this individual values of the result into a total. I tried this, but it is returning a big number (not correct):
select sum(value1)
from `digiwork_esp-data`.SensorData
where value1 = any (SELECT max(value1) FROM `digiwork_esp-data`.SensorData group by sensor)
You can directly sum the result of the subquery.
select sum(t1.val) from
(select max(value1) val FROM digiwork_esp-data.SensorData group by sensor) t1

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

Count number of rows with specific values

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;

Return the values of max count of rows with the same values

I have table_schedule(id,instructorNumber,time,date,cpNumber,user_id);
Is it possible to output the values of instructorNumber,time, etc. with the same highest occurence with same values?
sorry I am new in sql, so I am hoping that someone can help me to the query
Just group by all the fields you need for "same value comparison", order by count desc (so the result with most occurences will be first), and take first.
select
instructorNumber, time, date, cpNumber
from table_schedule
group by instructorNumber, time, date, cpNumber
order by count(*) desc
LIMIT 1
you may use this as a join on a main query if you want more than one result.
First group by the values you want to compare by and count. Get the maximum count (in MySQL you can use LIMIT for that). Then do the same group-by query again and take only the results HAVING the maximum count. Use GROUP_CONCAT to get a list of IDs in a string.
select instructorNumber, time, date, cpNumber, user_id, group_concat(id) as ids
from table_schedule
group by instructorNumber, time, date, cpNumber, user_id
having count(*) =
(
select count(*)
from table_schedule
group by instructorNumber, time, date, cpNumber, user_id
order by count(*) desc limit 1
);

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