简体   繁体   中英

MYSQL: Setting column value based on count grouped by other columns

I have the table, which has following Columns:

+-----------------------------+-------+---------------+----------------+
| School_Name                 | Class | ClassFiveSize | ClassEightSize |
+-----------------------------+-------+---------------+----------------+
| Tando Ghulam Ali-I          |     5 |          NULL |           NULL |
| Tando Ghulam Ali-I          |     5 |          NULL |           NULL |
| Tando Ghulam Ali-I          |     8 |          NULL |           NULL |
| Model School (E.M) Larkano. |     5 |          NULL |           NULL |
| Model School (E.M) Larkano. |     5 |          NULL |           NULL |
| Model School (E.M) Larkano. |     8 |          NULL |           NULL |
| Model School (E.M) Larkano. |     5 |          NULL |           NULL |
| Model School (E.M) Larkano. |     8 |          NULL |           NULL |

I want to set values in ClassFiveSize & ClassEightSize columns based on the count of School names, like:

+-----------------------------+-------+---------------+----------------+
| School_Name                 | Class | ClassFiveSize | ClassEightSize |
+-----------------------------+-------+---------------+----------------+
| Tando Ghulam Ali-I          |     5 |          2    |           1    |
| Tando Ghulam Ali-I          |     5 |          2    |           1    |
| Tando Ghulam Ali-I          |     8 |          2    |           1    |
| Model School (E.M) Larkano. |     5 |          3    |           2    |
| Model School (E.M) Larkano. |     5 |          3    |           2    |
| Model School (E.M) Larkano. |     8 |          3    |           2    |
| Model School (E.M) Larkano. |     5 |          3    |           2    |
| Model School (E.M) Larkano. |     8 |          3    |           2    |

Please let me know how to do it.

I am doing something like this:

update tableName t
inner join (
    select School_Name, count(*) counter
    from tableName
    where Class=8  group by School_Name ) g on g.School_Name = t.School_Name
set t.ClassEightSize = case when t.Class=8 then g.counter else 0 end;

You can use conditional aggregation in a query that returns the counters for each case and join it to the table:

update tablename t inner join ( 
  select School_Name, 
    sum(Class = 5) counter5,
    sum(Class = 8) counter8
  from tablename  
  group by School_Name 
) g on g.School_Name = t.School_Name 
set
  t.ClassFiveSize = g.counter5,
  t.ClassEightSize = g.counter8;

See the demo .
Results:

| School_Name                 | Class | ClassFiveSize | ClassEightSize |
| --------------------------- | ----- | ------------- | -------------- |
| Tando Ghulam Ali-I          | 5     | 2             | 1              |
| Tando Ghulam Ali-I          | 5     | 2             | 1              |
| Tando Ghulam Ali-I          | 8     | 2             | 1              |
| Model School (E.M) Larkano. | 5     | 3             | 2              |
| Model School (E.M) Larkano. | 5     | 3             | 2              |
| Model School (E.M) Larkano. | 8     | 3             | 2              |
| Model School (E.M) Larkano. | 5     | 3             | 2              |
| Model School (E.M) Larkano. | 8     | 3             | 2              |

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