Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/10/2011, 18:43
victorfz
 
Fecha de Ingreso: abril-2002
Mensajes: 186
Antigüedad: 22 años
Puntos: 2
Como realizar esta consulta

Hola tengo esta consulta que me muestra un ranking ordenado por la puntuación de los usuarios y también muestra el nombre del usuario y su posición.
Lo que necesito que que solo tenga en cuenta los registros de la tabla ranking cuyo IdTest tenga un IdFamilias=x
Lo he intentado poniendo un inner join a la tabla test y luego poniendo un WHERE IdFamilias=x pero no me funciona debido a la clausula GROUP BY.

Alguna sugerencia o comentario que me pueda servir de ayuda??


SELECT Usuario, IdUsuario, sum( `Puntos` ) AS SumaDePuntos
FROM `ranking`
INNER JOIN usuarios ON usuarios.Id = ranking.IdUsuario
GROUP BY IdUsuario
ORDER BY SumaDePuntos DESC

Adjunto estructura de tablas reducidas
Gracias de antemano y un saludo.



CREATE TABLE IF NOT EXISTS `ranking` (
`Id` int(11) NOT NULL auto_increment,
`IdUsuario` int(11) NOT NULL,
`IdTest` int(11) NOT NULL,
`TipoTest` int(11) NOT NULL,
`Fecha` int(11) NOT NULL,
`Puntos` int(11) NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=132 ;



CREATE TABLE IF NOT EXISTS `test` (
`Id` int(11) NOT NULL auto_increment,
`Titulo` varchar(50) NOT NULL,
`TipoTest` int(11) NOT NULL ,
`IdFamilias` int(11) NOT NULL
KEY `Id` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=179 ;

CREATE TABLE IF NOT EXISTS `test` (
`Id` int(11) NOT NULL auto_increment,
`Titulo` varchar(50) NOT NULL,
`TipoTest` int(11) NOT NULL ,
`IdFamilias` int(11) NOT NULL
KEY `Id` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=179 ;



CREATE TABLE IF NOT EXISTS `testfamilias` (
`Id` int(11) NOT NULL auto_increment,
`Nombre` varchar(50) NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;