Home > Back-end >  I need the query to return the amount of each ceoModel_id present in the table below, something that
I need the query to return the amount of each ceoModel_id present in the table below, something that

Time:03-14

| ceoModel_id | Qt_ceoModel |
|:----------- |------------:|
| 2           | 1           |
| 3           | 2           |
| 4           | 2           |

original table

CodePudding user response:

select ceoModel_id, 
       count(ceoModel_id) as Qt_ceoModel
from   inventory_ceos
group by ceoModel_id;

This works?

CodePudding user response:

You nbeed to GROUP BY ceoModel_id

CREATE tABLE inventory_ceos (ceoModel_id int)
INSERT INTO inventory_ceos VALUES(1),(2),(2),(3),(3)
SELECT ceoModel_id, COUNT(*) AS Qt_ceoModel  FROM inventory_ceos GROUP BY ceoModel_id
ceoModel_id | Qt_ceoModel
----------: | ----------:
          1 |           1
          2 |           2
          3 |           2

db<>fiddle here

  • Related