SQL Query to Group Records in MS-ACCESS

Asked

Viewed 290 times

1

Hello, I would like an SQL command for a query in a Access.
The structure of the table is:
tbList

Id | Nome | Genero | Endereco | Estado | ...

The result I would like to group by state and count the amount of genders (sex) in this way:

+-----------+----------+-----------+
| ESTADO    | FEMININO | MASCULINO |
+-----------+----------+-----------+
| SP        |       36 |        40 |
| RJ        |       44 |        13 |
| MG        |       17 |        23 |
...
...

Thank you in advance for your attention!

PS.: Sorry for anything wrong I’ve done, but I’m still learning how to use the stackoverflow!

1 answer

1


I think this syntax is accepted in MS-ACCESS:

SELECT      Estado
        ,   SUM(IIF(Genero = 'F', 1, 0)) AS FEMININO
        ,   SUM(IIF(Genero = 'M', 1, 0)) AS MASCULINO
FROM        tbList
GROUP BY    Estado

I assumed in the column Genero were the values 'F' and 'M', otherwise just change to the actual values.

  • Thank you! That’s right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.