List sub-query table with main query

Asked

Viewed 1,533 times

1

SGDB: Mysql.

PROBLEM: I need to somehow relate the subquery (being table and highlighted field Funcionario.codZona1) with the table "Zone", which is in the main query. Is there any way?


Table "ZONE", as follows: Contains all the City Zones.

Fields in the table: "code" (INT - key - autoincrement - table identifier code), nameZona (VARCHAR - name of Zone)

Data: Code | Area name

1 | Zona Centro-Oeste 
2 | Zona Centro-Sul 
3 | Zona Leste 
4 | Zona Norte 
5 | Zona Oeste 
6 | Zona Sul 

Tables "ITINERARIO", "ITINERARIODEPENDENTE", "ITINERARIOEXTRA" (ALL EQUAL): Contains list of employees and Areas where they live.

Fields in the table: "Code" (INT - Key - autoincrement), "employee code" (INT - employee code), "codZona1" (INT - Zone code where employee is allocated)

Data: Code | codFunctioning | codZona1

1 | 1 | 5 
2 | 2 | 1
3 | 3 | 4
4 | 4 | 3
5 | 5 | 5
6 | 6 | 6
7 | 7 | 2
8 | 8 | 4
9 | 9 | 5 

The query below tries to display the amount of "functionality" (Made by COUNT )existing in the tables "ITINERARIO", "ITINERARIODEPENDENTE" AND "ITINERARIOEXTRA" that are being unified by UNION, being summed in a Sub Query in the excerpt "SELECT SUM(qtdPeople) AS ttPeople FROM (". And in a main query (which is above the SUM of "qtdPeople"), is the "nomeZona", which should be directly related to the tables "ITINERARIO", "ITINERARIODEPENDENTE" AND "ITINERARIOEXTRA" by the fields "codZona1" with the field "codigo" of the table "Zone".

    SELECT Zona.nomeZona, ( 
        SELECT SUM(qtdPessoas) AS ttPessoas FROM (
            SELECT      COUNT(DISTINCT Itinerario.codFuncionario) AS qtdPessoas
            FROM        Itinerario
            WHERE       Itinerario.codZona1 = Zona.codigo  
            UNION ALL
            SELECT      COUNT(DISTINCT ItinerarioDependente.codDependente) AS qtdPessoas
            FROM        ItinerarioDependente
            WHERE       ItinerarioDependente.codZona1 = Zona.codigo  
            UNION ALL
            SELECT      COUNT(DISTINCT ItinerarioExtra.codExtra) AS qtdPessoas
            FROM        ItinerarioExtra 
            WHERE       ItinerarioExtra.codZona1 = Zona.codigo)
        AS tbP1) AS QtdFuncionarios
    FROM Zona

'Cause the way I’m doing it, it’s working like this:

Zona Centro-Oeste   900
Zona Centro-Sul     900
Zona Leste          900
Zona Norte          900
Zona Oeste          900
Zona Sul            900

Instead of coming like this:

Zona Centro-Oeste   100
Zona Centro-Sul     200
Zona Leste          100
Zona Norte          400
Zona Oeste          50
Zona Sul            50

1 answer

1

 SELECT Zona.nomeZona, ( 
        SELECT SUM(qtdPessoas) AS ttPessoas FROM (
            SELECT      COUNT(DISTINCT Itinerario.codFuncionario) AS qtdPessoas,
                        Itinerario.codZona1 
            FROM        Itinerario
            GROUP BY       Itinerario.codZona1  
            UNION ALL
            SELECT      COUNT(DISTINCT ItinerarioDependente.codDependente) AS qtdPessoas,
                        ItinerarioDependente.codZona1
            FROM        ItinerarioDependente
            GROUP BY  ItinerarioDependente.codZona1
            UNION ALL
            SELECT      COUNT(DISTINCT ItinerarioExtra.codExtra) AS qtdPessoas,
                        ItinerarioExtra.codZona1
            FROM        ItinerarioExtra 
            GROUP BY       ItinerarioExtra.codZona1)
        AS tbP1
        WHERE tbP1.codZona1 = Zona.codigo) AS QtdFuncionarios
    FROM Zona

As our friend above said would be so consultation;

Browser other questions tagged

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