SQL query by data type

3

I have the following query:

     SELECT 

       COUNT(co.sexo_id) as TOTALDIA, se.SEXO
FROM 
       CONDUCTORES co
       JOIN TRAMITE tr ON co.conductor_id=tr.conductor_id
       join LICENCIAS lic on tr.licencia_id=lic.licencia_id
       join CAT_SEXO se on se.SEXO_ID=co.sexo_id      
WHERE   

       YEAR(fecha_captura) = YEAR(GETDATE())
       AND MONTH(fecha_captura) = MONTH(GETDATE())

        GROUP BY se.SEXO ORDER BY TOTALDIA ASC, SEXO asc

This brings me this:

I would like to get something like this:

Annex the following Data For the suggestion that makes me @Mauricio Arias Olave

My table CAT_SEXO gives me the Type of SEX and is related to CONDUCTORS through the field sex_id which is a field of binaries if it is 1 = FEMALE if it is 0 = MALE the table CONDUCTORS is related to the table TRAMITES that only serves me for Relationship with LICENSES from which I obtain the Date. and the counting I do based on the field sex_id of the CONDUCTORES Table.

Those are the Data of My tables I also hope that explains why I use all the tables.  The query it brings is correct, but what I'm looking for is that the data from the SEXO Pertaining Field are in Different Fields @gbianchi, I was testing with LIKE and similar but I do not get it, that's why I resort to this Master zone, I think it should be a comparison based on the SEX_ID field of my CAT_SEXO table which is a fixed table from there I take the data.

    
asked by Abraham 05.09.2018 в 19:30
source

3 answers

2

I think the most direct way is using a conditional sum:

SELECT  COUNT(1)                                             as TOTALDIA
        COUNT(CASE WHEN co.sexo_id = 1 THEN 1 ELSE NULL END) as SEXOFEMENINO,
        COUNT(CASE WHEN co.sexo_id = 0 THEN 1 ELSE NULL END) as SEXOMASCULINO
        FROM CONDUCTORES AS co INNER JOIN TRAMITE AS tr 
        ON co.conductor_id = tr.conductor_id INER JOIN LICENCIAS AS lic 
        ON tr.licencia_id = lic.licencia_id INNER JOIN CAT_SEXO AS se 
        ON se.SEXO_ID = co.sexo_id      
        WHERE YEAR(fecha_captura) = YEAR(GETDATE()) 
        AND MONTH(fecha_captura) = MONTH(GETDATE())

The query is now not subject to sex (we remove the filter in the WHERE ), but what is conditioned is the sum.

    
answered by 07.09.2018 / 05:11
source
4

One way to solve this, although nothing performant, would be to make the 3 sums separately in a single query.

First, let's get a query that tells one type or another, that would be something like this:

CONSULTA A:
SELECT 
    COUNT(co.sexo_id)
FROM 
    CONDUCTORES co
    JOIN TRAMITE tr ON co.conductor_id=tr.conductor_id
    join LICENCIAS lic on tr.licencia_id=lic.licencia_id
    join CAT_SEXO se on se.SEXO_ID=co.sexo_id      
WHERE   
    YEAR(fecha_captura) = YEAR(GETDATE())
    AND MONTH(fecha_captura) = MONTH(GETDATE())
    AND co.sexo_id = 0

This query will tell all the males. If in the where we change co.sexo_id = 1 , it will count the feminine ones.

That is, there we have the two accounts.

Since sqlserver supports selects without from, you might try doing something like this:

CONSULTA C:
Select (CONSULTA A) as masculino, (CONSULTA B) as femenino

query B, would be equal to A, but looking for the other sex.

That, we will return a table with two columns, one for male and one for female. Now we just have to get the sum too .. and for that, we wrap the previous query in another:

SELECT *, masculino+femenino as total
FROM (consulta C)

In this way, you get what you want.

    
answered by 05.09.2018 в 20:32
1

This may be an option:

  • You declare 2 variables that will store the results of the count or sum for each sex.
  • You make the query (as you explain in your question) it works, but you add WHERE to co.sexo_id .
  • You make a SELECT to bring the results as you wish.

This would be the result of my explanation:

-- Variables que guardarán la cantidad (SUM o COUNT) según el sexo.
DECLARE @cantidad_datos_femenino AS INT = 0;
DECLARE @cantidad_datos_masculino AS INT = 0;

-- Establecer la cantidad de datos en las variables.
-- Femenino:
SET @cantidad_datos_femenino = (SELECT COUNT(co.sexo_id) as TOTALDIA
                                FROM CONDUCTORES AS co INNER JOIN TRAMITE AS tr 
                                ON co.conductor_id = tr.conductor_id INER JOIN LICENCIAS AS lic 
                                ON tr.licencia_id = lic.licencia_id INNER JOIN CAT_SEXO AS se 
                                ON se.SEXO_ID = co.sexo_id      
                                WHERE YEAR(fecha_captura) = YEAR(GETDATE()) 
                                AND MONTH(fecha_captura) = MONTH(GETDATE())
                                AND co.sexo_id = 1 /* FEMENINO. */);

-- Masculino:
SET @cantidad_datos_masculino = (SELECT COUNT(co.sexo_id) as TOTALDIA
                                 FROM CONDUCTORES AS co INNER JOIN TRAMITE AS tr 
                                 ON co.conductor_id = tr.conductor_id INER JOIN LICENCIAS AS lic 
                                 ON tr.licencia_id = lic.licencia_id INNER JOIN CAT_SEXO AS se 
                                 ON se.SEXO_ID = co.sexo_id      
                                 WHERE YEAR(fecha_captura) = YEAR(GETDATE()) 
                                 AND MONTH(fecha_captura) = MONTH(GETDATE())
                                 AND co.sexo_id = 0 /* MASCULINO. */);

-- Esta es la consulta con los resultados esperados:
SELECT SUM(@cantidad_datos_femenino + @cantidad_datos_masculino) AS [TOTAL DIA],
       @cantidad_datos_femenino AS [SEXOFEMENINO],
       @cantidad_datos_masculino AS [SEXOMASCULINO]
    
answered by 05.09.2018 в 20:32