1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to order by and group by equal column values in SQL? [duplicate]

Discussão em 'Outras Linguagens' iniciado por Stack, Julho 12, 2021.

  1. Stack

    Stack Membro Participativo

    My table is:

    device_name | device_category
    ---------------------------------
    standard_spot | light
    mobile_spot | light
    tracker | light
    smoking_machine| light
    microphone1 | sound
    microphone2 | sound
    display1_hmdi | projector
    display1_vga | projector
    display1_sound | projector


    I need a query that provides the device_names together with the device_categories ordered by the occurrence of a device_category. My attempt was this query, which provides the occurrences of each category:

    SELECT COUNT(*) AS num_dcats FROM `devices` GROUP BY device_category ORDER BY num_dcats DESC


    Like this:

    device_category | num_dcats
    ---------------------------
    light | 4
    projector | 3
    sound | 2

    Continue reading...

Compartilhe esta Página

Para os pais de Plantão algumas dicas