[SOLUCIONADO] Pivot tabla Mysql

Iniciado por REMHINOB, Mayo 13, 2019, 02:03:31 PM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Mayo 13, 2019, 02:03:31 PM Ultima modificación: Mayo 14, 2019, 12:32:14 AM por Gabriela
Hola, necesito su ayuda por favor, tengo una tabla cuya estructura es asi:
id_tareo id_personal fec_tareopers   id_codtareo
000001     23         01/05/2019    TD
000002     23         02/05/2019    TD
000003     23         03/05/2019    TN
000004     12         01/05/2019    F
000005     12         02/05/2019    TN
000006     12         03/05/2019    TN

y deseo que mi resultado sea algo asi:

id_personal 01/05/2019  02/05/2019 03/05/2019     TD  TN   F
    23                   TD             TD        TN                  2    1    0
    12                    F              TN        TN                  0    2    1

intente con esta linea pero solo me llena la primera columna (01/05/2019):

SELECT  id_personal, 
(CASE WHEN AK.fec_tareopers = '01/05/2019' THEN You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_codtareo ELSE NULL END)  '01/05/2019',
(CASE WHEN AK.fec_tareopers = '02/05/2019' THEN You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_codtareo ELSE NULL END)  '02/05/2019'
(CASE WHEN AK.fec_tareopers = '03/05/2019' THEN You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_codtareo ELSE NULL END)  '02/05/2019'
from adm_tareopers AK
inner join adm_personal AP on You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal=AP.id_personal
group by You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal

de antemano gracias por su ayuda

Mayo 13, 2019, 03:16:22 PM #1 Ultima modificación: Mayo 13, 2019, 03:32:03 PM por Solid Water
Prueba así:

SELECT  id_personal, 
(select id_codtareo from adm_tareopers where fec_tareopers = '01/05/2019' and id_personal = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal) as '01/05/2019',
(select id_codtareo from adm_tareopers where fec_tareopers = '02/05/2019' and id_personal = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal) as '02/05/2019',
(select id_codtareo from adm_tareopers where fec_tareopers = '03/05/2019' and id_personal = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal) as '03/05/2019'
from adm_tareopers AK
inner join adm_personal AP on You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal=AP.id_personal
group by You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal

Saludos,

Mayo 13, 2019, 07:46:05 PM #2 Ultima modificación: Mayo 14, 2019, 12:31:43 AM por Gabriela
You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login
Prueba así:

SELECT  id_personal, 
(select id_codtareo from adm_tareopers where fec_tareopers = '01/05/2019' and id_personal = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal) as '01/05/2019',
(select id_codtareo from adm_tareopers where fec_tareopers = '02/05/2019' and id_personal = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal) as '02/05/2019',
(select id_codtareo from adm_tareopers where fec_tareopers = '03/05/2019' and id_personal = You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal) as '03/05/2019'
from adm_tareopers AK
inner join adm_personal AP on You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal=AP.id_personal
group by You are not allowed to view links. You are not allowed to view links. Register or Login or You are not allowed to view links. Register or Login_personal

Saludos,


ERES UN GENIO!!!!!, Gracias de corazon hemano

Gracias.
Si el usuario de la app se confunde y para el mismo id y misma fecha crea 2 o más tareopers la consulta fallará. Para evitar eso o declaras los 3 campos como primary key compuesta o haces un filtro que primero busque que ya no exista y si existe no lo permita o haga un update en lugar de un insert o le dices que solo traiga la primer ocurrencia en el sql agregando limit 1 dentro de cada subconsulta de los parentesis. Eso último filtraría el error pero quedaría la basura en la base de datos.