Home > database >  Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, &l
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, &l

Time:11-03

DECLARE @Recaudacion as INT
DECLARE @Division as INT

SELECT @Recaudacion = (SELECT SUM(pelicula.PrecioEntrada) 
                       FROM pelicula) 

SELECT @Division = (SELECT COUNT(*) 
                    FROM funcion  
                    GROUP BY NombrePelicula 
                    HAVING COUNT(*) > 1)

SELECT 
    (@Recaudacion / @Division) AS Recaudacion, 
    funcion.NombrePelicula 
FROM funcion 

I get this message

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

I'm expecting the average price for each show

CodePudding user response:

SELECT count(*) FROM funcion GROUP BY NombrePelicula

Will return the count for each NombrePelicula so it can be more than one value.

You could try:

DECLARE @Recaudacion As integer = (SELECT SUM(pelicula.PrecioEntrada) FROM pelicula) 
SELECT @Recaudacion / count(*) as Recaudacion, NombrePelicula 
FROM funcion  
GROUP BY NombrePelicula 
HAVING COUNT(*) > 1

CodePudding user response:

I see this in a comment:

i need more than one value

Okay. Let's run this as a real query then:

SELECT NombrePelicula, 
    (SELECT SUM(pelicula.PrecioEntrada) FROM pelicula) / count(*) As Recaudacion
FROM funcion  
GROUP BY NombrePelicula 
HAVING COUNT(*)>1
  • Related