MySQL Forums
Forum List  »  Spanish

Error MYSQL - Consulta con referencias cruzadas
Posted by: Enrico Renato Salcedo Herrera
Date: March 28, 2009 07:28PM

Espero me equivoque pero no encuentro posible solución a este problema, espero me puedan ayudar y descartar una posible debilidad propia de Base de Datos MYSQL. Lo describo a continuación.

Tengo la siguiente tabla:

+--------------+------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+------------+-------+
| FechaDis | date | NO | PRI | 0000-00-00 | |
| DNIPI | varchar(8) | NO | PRI | | |
| Disponible | char(1) | YES | | NULL | |
| FechaHoraDDI | datetime | YES | | NULL | |
+--------------+------------+------+-----+------------+-------+

Los datos en esta tabla:

+------------+----------+------------+---------------------+
| FechaDis | DNIPI | Disponible | FechaHoraDDI |
+------------+----------+------------+---------------------+
| 2009-04-05 | 00253655 | 1 | 2009-03-28 19:28:44 |
| 2009-04-06 | 00253655 | 0 | 2009-03-28 19:28:24 |
| 2009-04-07 | 00253655 | 1 | 2009-03-28 17:26:43 |
+------------+----------+------------+---------------------+

Lo que dese generar es:

+----------+---------+---------+---------+
| DNIPI | 05Abril | 06Abril | 07Abril |
+----------+---------+---------+---------+
| 00253655 | 1 | 0 | 1 |
+----------+---------+---------+---------+

Mi primer intento fue generar la consulta cruzada como lo hecho antes en SQL:

SELECT
detalledi.DNIPI,
(CASE WHEN detalledi.FechaDis='2009-04-06' THEN detalledi.disponible END) AS 06Abril,
(CASE WHEN detalledi.FechaDis='2009-04-05' THEN detalledi.disponible END) AS 05Abril,
(CASE WHEN detalledi.FechaDis='2009-04-07' THEN detalledi.disponible END) AS 07Abril
FROM
detalledi
GROUP BY
detalledi.DNIPI

Resultado:

+----------+---------+---------+---------+
| DNIPI | 06Abril | 05Abril | 07Abril |
+----------+---------+---------+---------+
| 00253655 | NULL | 1 | NULL |
+----------+---------+---------+---------+

Investigando encontre un post que mostraba la solución, pero a aplicarlo tampoco funciono.

SELECT DNIPI,
( SELECT (CASE FechaDis WHEN '2009-04-05' THEN Disponible END) AS '05Abril' FROM
detalledi WHERE FechaDis=padre.FechaDis AND (CASE FechaDis WHEN '2009-04-05' THEN Disponible END))AS '05Abril',
( SELECT (CASE FechaDis WHEN '2009-04-06' THEN Disponible END) AS '06Abril' FROM
detalledi WHERE FechaDis=padre.FechaDis AND (CASE FechaDis WHEN '2009-04-06' THEN Disponible END))AS '06Abril',
( SELECT (CASE FechaDis WHEN '2009-04-07' THEN Disponible END) AS '07Abril' FROM
detalledi WHERE FechaDis=padre.FechaDis AND (CASE FechaDis WHEN '2009-04-07' THEN Disponible END))AS '07Abril'
FROM detalledi padre GROUP BY DNIPI ORDER BY DNIPI

Resultado:

+----------+---------+---------+---------+
| DNIPI | 05Abril | 06Abril | 07Abril |
+----------+---------+---------+---------+
| 00253655 | 1 | NULL | NULL |
+----------+---------+---------+---------+
1 row in set

Observación:

En la segunda solución recorria un nuevo SELECT independientemente para asegurarme que tome el valor, pero no funcionó.

- MYSQL solo esta tomando el primer registro que encuentra por cada DNI, los demas los pone no encontrados osea NULL, por mas que hice el recorrido independiente, espero me faciliten una solucion.

Options: ReplyQuote


Subject
Views
Written By
Posted
Error MYSQL - Consulta con referencias cruzadas
6742
March 28, 2009 07:28PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.