Help with query sql server 2008 pls

3

I have a table called usuarios which has several fields, one of them is the field estado_usuario which has as parameter 0, means the user is disabled and 1 which means that the user is valid.

I need to count the number of current users and not valid but I do not know if it is possible to show both of them in 1 query.

for now I can only get in 2 query the respective amount of each state of validity

SELECT COUNT(ESTADO_USUARIO) FROM USUARIOS WHERE ESTADO_USUARIO= 0; <--- (me da 150)

SELECT COUNT(ESTADO_USUARIO) FROM USUARIO WHERE ESTADO_USUARIO= 1; <----- (me da 320)

The idea is as I said earlier to get both results in 1 query, but I do not know how I could do it ... it could also be a stored procedure.

My database engine is SQL SERVER 2008.

    
asked by Jordan Blake Told 01.10.2018 в 17:40
source

3 answers

0
SELECT estado_usuario, count(estado_usuario) from usuarios group by estado_usuario

You have to use GROUP BY that groups the rows selected by the fields that appear in the GROUP BY clause. These groups return a single row per group, in this case you would return each state with its total respectively.

    
answered by 01.10.2018 / 17:43
source
4

You should investigate about using GROUP BY in aggregation functions. In this case, the query would be simply:

SELECT  Estado_Usuario,
        COUNT(*) Usuarios
FROM dbo.Usuarios
GROUP BY Estado_Usuario
;
    
answered by 01.10.2018 в 17:42
0

To be able to do what they need, it is necessary to understand the use of groupers such as group by and HAVING, as well as aggregation functions such as COUNT, MAX, AVGT, etc. In this case you need to add and group the results, so it will show you the sum of the different statuses that exist.

SELECT estado_usuario, COUNT(estado_usuario) 
    from usuarios 
group by estado_usuario.

I leave a link for future references

link

    
answered by 01.10.2018 в 20:59