Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql group by sub group

Tags:

sql

I am trying to work out the majority sub group of a group. For an example let's say my table looks like this:

+--------------------------------------------------+
|   city      |  car_colour |  car_type |  qty   |
+--------------------------------------------------+
| ------------------------------------------------ |
| manchester  |  Red        |  Sports   |  7       |
| manchester  |  Red        |  4x4      |  9       |
| manchester  |  Blue       |  4x4      |  8       |
| london      |  Red        |  Sports   |  2       |
| london      |  Blue       |  4x4      |  3       |
| leeds       |  Red        |  Sports   |  5       |
| leeds       |  Blue       |  Sports   |  6       |
| leeds       |  Blue       |  4X4      |  1       |
+--------------------------------------------------+

I've tried to find a pure sql solution so that i can see: in each city, which colour of car has the largest quantity.

I can do:

select city, cars, sum(qty)
from table
group by city, cars

to get:

+------------+------+----+
| manchester | red  | 16 |
| manchester | blue |  8 |
| london     | red  |  2 |
| london     | blue |  3 |
| leeds      | red  |  5 |
| leeds      | blue |  7 |
+------------+------+----+

but is there anyway i can use a sub query to get the max of the results which would return the max colour for each city, so the results would show:

+------------+------+
| manchester | red  |
| london     | blue |
| leeds      | blue |
+------------+------+

I can do these calculations in my Python script but would prefer a pure SQL solution.

Hope this makes sense, thanks for any help in advance :)

Tommy

like image 284
teebagz Avatar asked Apr 03 '15 10:04

teebagz


People also ask

What is group by in SQL Server?

SELECT - GROUP BY- Transact-SQL. A SELECT statement clause that divides the query result into groups of rows, usually for the purpose of performing one or more aggregations on each group. The SELECT statement returns one row per group.

How to get total records for each subgroup in SQL?

Now we will try to apply group by command including two different fields and we will break the groups and get total records for each sub-group also. This way we will get total of each subgroup also. This can be further improved by adding SQL HAVING command with the group by command.

How do you use group by in SQL without aggregate?

SQL GROUP BY and DISTINCT If you use the GROUP BY clause without an aggregate function, the GROUP BY clause behaves like the DISTINCT operator. The following gets the phone numbers of employees and also group rows by the phone numbers. SELECT phone_number FROM employees GROUP BY phone_number;

How do I combine multiple Group by clauses in SQL?

GROUP BY GROUPING SETS () The GROUPING SETS option gives you the ability to combine multiple GROUP BY clauses into one GROUP BY clause. The results are the equivalent of UNION ALL of the specified groups. For example, GROUP BY ROLLUP (Country, Region) and GROUP BY GROUPING SETS (ROLLUP (Country, Region)) return the same results.


1 Answers

select distinct p.city, p.car_colour,sq.qty as qty
from         ( select t.car_colour,t.city, sum(t.qty) as qty
                from table1 t
                group by t.car_colour,t.city
             )p
join         ( select q.city,max(q.qty) qty from
                  ( select t.car_colour,t.city, sum(t.qty) as qty
                   from table1 t
                   group by t.car_colour,t.city
                  )q
                 group by q.city
              )sq
on p.city=sq.city and p.qty=sq.qty 

SQL FIDDLE DEMO

like image 166
nil Avatar answered Oct 12 '22 10:10

nil