Tema: Consulta sql
Ver Mensaje Individual
  #2 (permalink)  
Antiguo 18/11/2008, 02:25
quimfv
Colaborador
 
Fecha de Ingreso: marzo-2008
Ubicación: Sabadell
Mensajes: 4.897
Antigüedad: 16 años, 9 meses
Puntos: 574
Respuesta: Consulta sql

No lo he usado nunca pero mira lo que dice el manual de Mysql


Cita:
COUNT(DISTINCT expr,[expr...])

Returns a count of the number of different non-NULL values.

COUNT(DISTINCT) returns 0 if there were no matching rows.

mysql> SELECT COUNT(DISTINCT results) FROM student;
In MySQL, you can obtain the number of distinct expression combinations that do not contain NULL by giving a list of expressions. In standard SQL, you would have to do a concatenation of all expressions inside COUNT(DISTINCT ...).
Con SQL standar...???...

Código sql:
Ver original
  1. SELECT `Socio`, `Clubs`, `Deportes`
  2.    FROM
  3.       (SELECT `subClubs`.`IDSocio` AS `Socio`, COUNT(*) AS `Clubs`
  4.          FROM (SELECT DISTINCT `tuTabla`.`IDSocio`, `tuTabla`.`IDClub` AS `Club`
  5.                         FROM `tuTabla`) AS `subClubs`
  6.          GROUP BY `subClubs`.`IDSocio`
  7.          HAVING COUNT(*)>5) AS SocioClub
  8.    INNER JOIN
  9.       (SELECT `subDeportes`.`IDSocio` AS `Socio`, COUNT(*) AS `Deportes`
  10.           FROM (SELECT DISTINCT `tuTabla`.`IDSocio`, `tuTabla`.`IDDeporte` AS `Deporte`
  11.                        FROM `tuTabla`) AS `subDeportes`
  12.           GROUP BY `subDeportes`.`IDSocio`
  13.           HAVING COUNT(*)>5) AS SocioDep
  14.    ON  SocioClub.Socio=SocioDep.Socio;

Quim

Última edición por quimfv; 18/11/2008 a las 02:36