简体   繁体   中英

Mysql subquery for group and count records by specific field

I have this problem:

I have a car table, each car record has a state field, state field value can be:

1 = Enable or 2 = Disable

For example, in this case I need to show all cars grouped by color field and counted by color , this is not problem really :)

Here the SQL statement :

SELECT 
  `id`, 
  `model_family`, 
  `color`, 
  COUNT(`color`) AS 'quantity',
  `state` 
FROM `auto` 
WHERE `model_family` = 'Sedan'
GROUP BY `color`

+-----+--------------+------------+----------+---+
| id  | model_family | color  | quantity | state |
+-----+--------------+--------+----------+-------+
|  77 | Sedan        | Red    |        2 |     2 |
|  42 | Sedan        | Blue   |        3 |     2 |
|  97 | Sedan        | Green  |        5 |     1 |
+-----+--------------+--------+----------+-------+

Results show two Disabled records and one Enabled record.

Well, the questions is :

How can I can show the cars disabled and enabled but For example, if for first grouped record if state is disabled ( state = 2) then quantity field will appear as "0" (because it not exists cars enabled) else quantity = n

Something like this:

+-----+--------------+------------+----------+-------+
| id  | model_family | color      | quantity | state |
+-----+--------------+------------+----------+-------+
|  77 | Sedan        | Red        |        0 |     2 |
|  42 | Sedan        | Blue       |        0 |     2 |
|  97 | Sedan        | Green      |        5 |     1 |
+-----+--------------+------------+----------+-------+

Regads !

SELECT 
 `id`, 
 `model_family`, 
 `color`, 
 SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END) as enabled,
 SUM(CASE WHEN state = 2 THEN 1 ELSE 0 END) as disabled,
 `state` 
FROM `auto` 
WHERE `model_family` = 'Sedan'
GROUP BY `color`

You have to use case statements to group the items together. When it finds that the state is 1 you will need to sum together all of those records which is why I am doing the Then 1 Else 0.

This was a quick example of how to do it. I haven't tested to make sure it works, but it should.

If you want all disabled groups to be replaced with 0, you can just put a single case statement in your select clause. Something like this:

SELECT id, model_family, color, (CASE WHEN state = 1 THEN COUNT(*) ELSE 0 END) AS quantity, state
FROM auto
WHERE model_family = 'Sedan'
GROUP BY color
ORDER BY id;

It tested fine with your data on SQL Fiddle .

EDIT Note, since you're grouping by color, you can just use COUNT(*) instead of COUNT(color) because they are going to count the same thing.

EDIT 2 Also important to note that if a color has some enabled vehicles and some disabled vehicles, it still returns 0 because there is at least one disabled. If you want a count of the enabled ones, you can do something like this:

SELECT id, model_family, color, SUM(CASE WHEN state = 1 THEN 1 ELSE 0 END) AS quantityEnabled, state
FROM auto
WHERE model_family = 'Sedan'
GROUP BY color
ORDER BY id;

This fiddle has both examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM