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
Related
I have a mysql table "lessons" from which I want to output a course list for each teacher (see picture).
Up to now I wanted to use GROUP BY and GROUP_CONCAT for the output, but I failed so far. I have no idea at the moment. Can anyone help me?
If I understand correctly, lessons that are at the same time (based on the last two columns) and for the same teacher and have the same name should be combined as a "course".
If this is the case, you can use two levels of aggregation:
select group_concat(ids) as ids,
group_concat(class_id) as class_ids,
teacher, name, weekday_hours
from (select l.class_id, l.teacher_id, l.name,
group_concat(l.weekday, ':', l.hour order by l.weekday, l.hour) as weekday_hours,
group_concat(l.id order by l.id) as ids
from lessons l
group by l.class_id, l.teacher_id, l.name
) l
group by teacher, name, weekday_hours;
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
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
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;
I have a businesses table. I want to get the owners name who has the most businesses. So far all I only know that I need to use GROUP BY and HAVING.
The problem is I only know the most basic queries...
Maybe something like this can help:
select owner, count(*) cntx
from businesses
group by owner
order by cntx desc
limit 1
Or executing the query without limit 1 clause, and then iterate the result till your needs are satisfied.
Use GROUP BY and order descending and then take the top one record which is the one that has most businesses:
select OwnerId, count(*) from businesses
group by OwnerId order by count(*) desc
limit 1