Ver Mensaje Individual
  #3 (permalink)  
Antiguo 05/07/2011, 09:31
leonardo_josue
Colaborador
 
Fecha de Ingreso: enero-2007
Ubicación: México
Mensajes: 2.097
Antigüedad: 17 años, 3 meses
Puntos: 447
Respuesta: Problema con mysql en mi consulta

Hola gcamasca:

Si entendí correctamente el problema lo único que tienes que hacer es agregar una condición NOT EXISTS especificando que no debe existir otro registro con un estado diferente en la tabla, sería más o menos así:

Código MySQL:
Ver original
  1. mysql> create table vendedores (Idventa int, Empresa varchar(15), Fecha date,
  2.     -> Estado varchar(15), Evento varchar(15), Idvendedor int);
  3. Query OK, 0 rows affected (0.06 sec)
  4.  
  5. mysql> insert into vendedores values
  6.     -> (1, 'ABB', '2011-06-21', 'Muy Probable',         'Conimera', 2),
  7.     -> (2, 'TELEFONICA', '2011-07-01', 'Muy Probable',  'Conimera', 2),
  8.     -> (3, 'ABB', '2011-07-01', 'Rechazado',    'Conimera', 2),
  9.     -> (4, 'FERREYROS', '2011-07-04', 'Muy Probable',   'Conimera', 2),
  10.     -> (5, 'BBVA', '2011-07-04', 'Aceptado',    'Conimera', 2);
  11. Query OK, 5 rows affected (0.05 sec)
  12. Records: 5  Duplicates: 0  Warnings: 0
  13.  
  14. mysql> select * from vendedores;
  15. +---------+------------+------------+--------------+----------+------------+
  16. | Idventa | Empresa    | Fecha      | Estado       | Evento   | Idvendedor |
  17. +---------+------------+------------+--------------+----------+------------+
  18. |       1 | ABB        | 2011-06-21 | Muy Probable | Conimera |          2 |
  19. |       2 | TELEFONICA | 2011-07-01 | Muy Probable | Conimera |          2 |
  20. |       3 | ABB        | 2011-07-01 | Rechazado    | Conimera |          2 |
  21. |       4 | FERREYROS  | 2011-07-04 | Muy Probable | Conimera |          2 |
  22. |       5 | BBVA       | 2011-07-04 | Aceptado     | Conimera |          2 |
  23. +---------+------------+------------+--------------+----------+------------+
  24. 5 rows in set (0.00 sec)
  25.  
  26. mysql> SELECT *
  27.     -> FROM vendedores V1
  28.     -> WHERE V1. idvendedor ='2' and
  29.     -> V1.Evento='Conimera' and
  30.     -> V1.Estado='Muy Probable' AND NOT EXISTS
  31.     -> (SELECT * FROM vendedores V2
  32.     -> where V1.Idventa != V2.Idventa and
  33.     -> V1.Empresa = V2.Empresa and
  34.     -> V1.Idvendedor = V2.Idvendedor and
  35.     -> V1.Evento = V2.Evento and
  36.     -> V2.Estado != 'Muy Probable');
  37. +---------+------------+------------+--------------+----------+------------+
  38. | Idventa | Empresa    | Fecha      | Estado       | Evento   | Idvendedor |
  39. +---------+------------+------------+--------------+----------+------------+
  40. |       2 | TELEFONICA | 2011-07-01 | Muy Probable | Conimera |          2 |
  41. |       4 | FERREYROS  | 2011-07-04 | Muy Probable | Conimera |          2 |
  42. +---------+------------+------------+--------------+----------+------------+
  43. 2 rows in set (0.00 sec)

dale un Vistazo para ver si es lo que necesitas.

Saludos
Leo.