MySQL Grouping using group_concat and then Group BY - mysql

I have a table like this:
Basically, I want to group all names and give the count of such groupings like:
Since there are 2 users with cooking and cleaning, 1 for washing and 4 for cooking.
I am trying to use group_concat
SELECT group_concat(DISTINCT name) AS name, count(*)
FROM warehouse.test1
GROUP BY guid
However, this will not work as I need to group by the groupings and then get count of those groupings.
I am not getting how to group on the groupings and then get the count.
Thanks in advance and I appreciate any heads up!
UPDATE
As per the answer I tried
SELECT groupings, COUNT(*)
FROM (SELECT group_concat(DISTINCT name) AS groupings
FROM warehouse.test1
GROUP BY saguid) t
GROUP BY groupings;
However, I get it as
# groupings, COUNT(*)
'cleaning,cooking', '2'
'cooking', '2'
'washing', '1'
shoudnt count be 4 for cooking?

I'd wrap this query with another query to coubt the grouped data:
SELECT groupings, COUNT(*)
FROM (SELECT group_concat(DISTINCT name) AS groupings
FROM warehouse.test1
GROUP BY saguid) t
GROUP BY groupings

Related

SQL How to Count table for each fields

I have 3 tables which is:
Courses
courses_id
name
QnAs
qna_id
student_id
courses_id
name
question
Students
student_id
name
Now I'm trying to count how many qna's there are for each courses. How do i make the query?
I've tried doing this :
SELECT (SELECT COUNT(qna_id) AS Expr1
FROM QnAs) AS Count
FROM QnAs AS QnAs_1 CROSS JOIN
Courses
GROUP BY Courses.courses_id
It does counts how many QnA's there are but not for each Courses
The output i got is each Courses names and QnAs count number but what i want is the QnA's number for each of the Courses
It seems you merely want to aggregate QNAs by course ID:
select courses_id, count(*)
from qnas
group by courses_id
order by courses_id;
Along with the course names:
select c.course_id, c.name, coalesce(q.cnt, 0) as qna_count
from courses c
left join
(
select courses_id, count(*) as cnt
from qnas
group by courses_id
) q on q.course_id = c.course_id
order by c.course_id;
Why not just use GROUP BY?
SELECT q.courses_id, COUNT(qna_id) as cnt
FROM QnAs q
GROUP BY q.courses_id;
This is not an answer, but just an explanation what your query does.
In your own query you first cross join all QnAs with all courses for no apparent reason, thus getting all possible combinations. So with two courses, each with three QNAs (that makes six QNAs in total), you'd construct 2 x 6 = 12 rows.
For each of these rows you select the total number of rows in the QNA table, which is six in above example. So you'd select 12 rows, all showing the number 6.
But then you group by course ID, thus ending up with two rows only in my example. You should apply an aggregate function on your subquery, e.g. MAX or SUM, but you don't, which makes your query invalid (because you are dealing with many rows, but treat this as if it were a single value). MySQL however silently applies ANY_VALUE, so your query becomes:
SELECT
ANY_VALUE( (SELECT COUNT(*) FROM QnAs) ) AS Count
FROM QnAs AS QnAs_1
CROSS JOIN Courses
GROUP BY Courses.courses_id;
I hope this explanation helps you understand how joins and aggregation work. You may want to set ONLY_FULL_GROUP_BY mode (https://dev.mysql.com/doc/...) in order to have MySQL report the syntax error instead of silently "fixing" the query by applying ANY_VALUE.

mysql group by multiple

I'm not sure if this is specifically a group by question, as I've tried grouping this by multiple columns. Basic problem is a table like this:
I would like to get the sum of the order total_price for different countries, but grouped by the order_id. So for France the sum of total_price should be 8000 as two of the rows are for the same order. My sql is clearly wrong as I am not getting this.
SELECT sum(total_price) as total_price_per_country
FROM cars
WHERE (country IN ('France'))
group by order_id, country;
Nope, GROUP BY won't quite get you that. If you GROUP BY country you get one row per country.
There's no way to do this without a second query, so it'd have to be something like this:
SELECT *, (SELECT SUM(total_price) FROM cars AS c2 WHERE c2.country = cars.country) AS total_price_per_country
FROM cars
(An INNER JOIN to a second copy of the table would work too.)
select sum(total_price) from cars where country = 'France' group by order_id
Try above query

SELECT which should give the number of unique n in an 1:n realtionship

Hello i've got a short question.
For an exercise at the university i've got a full joined table.
In this table is a column called supplier and a column prduct_id.
Now i want to make a select which does the following:
I want to get the number of unique (DISTINCT) product_id's for each supplier
i've tried the following, but it give me the number of all row's grouped by the supplier
SELECT DISTINCT COUNT(`product_code`),`id_supplier` FROM `refactored_data`
GROUP BY `refactored_data`.`id_supplier` ORDER BY
`refactored_data`.`id_supplier` ASC
Try this:
SELECT COUNT(DISTINCT product_id), id_supplier
FROM refactored_data
GROUP BY id_supplier
So it seems your query is actually simpler than you think.
You are grouping by supplier and count products. Then you say you only want distinct results, which doesn't change anything, because your results are already unique.
Move the DISTINCT inside your COUNT, so as to count distinct values, rather then making your lines distinct.
SELECT COUNT(DISTINCT product_code), id_supplier
FROM refactored_data
GROUP BY id_supplier
ORDER BY id_supplier ASC;

Order by sum(value) sql

I am trying to get a list of blog_id and the sum of donations on that blog id from a table called donations. And I want to order it by the sum of donations. Basically I want to produce a list of blogs ranked by donations. Blogs are held in a different table referenced by blog_id.
This is what I have been trying but all it does is sum up all donations and produce 1 row. I don't understand what I did wrong here!
$donations_result = mysql_query("SELECT blog_id, sum(amount) FROM donations ORDER BY sum(amount)");
Donations table is a series of blog_ids and individual donations. So something like this:
blog_id--donation
1 ----------26
1 ----------1
2 ----------24
2 ----------12
You did not group the columns. You need to group it by non-aggregated column and in this case by blog_id
SELECT blog_id, sum(amount) TotalSum
FROM donations
GROUP BY blog_id
ORDER BY TotalSum
The reason why your query executed well without throwing an exception is because mysql permits to use aggregate function without specifying non-aggregated column in the GROUP BY clause.
see MySQL Extensions to GROUP BY
You're missing the GROUP BY operator:
SELECT blog_id, sum(amount)
FROM donations
GROUP BY blog_id
ORDER BY sum(amount)

Nested group by

I have a table that contains ages of people and a group they are in, but sometimes the group is not present.
I would like to get a total age per group, but if the group data is missing then I would like the name of the person and their age.
It is almost like a nested group. I want to group by the group name first, but if it isn't present then group by individual's name.
I hope that makes some sense! Any help greatly appreciated.
You might try a union...
SELECT Group, sum(Age)
FROM People
WHERE Group > ''
GROUP by Group
UNION
SELECT PersonName, Age
FROM People
WHERE Group is null