Atef Ibrahim
Atef Ibrahim

Reputation: 25

Subquery must return a single row

When I start this query I got this error:

AnalysisException: Subquery must return a single row: (SELECT round(c1.feb15 + c1.nov15 + c1.dec15) FROM suivisousse.compteur2015)

Query:

SELECT
    c2.compteur, 
    ((c1.feb15 + c1.nov15 + c1.dec15) * 100 / 
         (SELECT ROUND(c1.feb15 + c1.nov15 + c1.dec15) FROM compteur2015)) 
FROM
    compteur2015 c1, compteur c2 
WHERE
    c1.compteur = c2.compteur
GROUP BY
    c2.compteur

Upvotes: 1

Views: 744

Answers (4)

Gordon Linoff
Gordon Linoff

Reputation: 1269803

You are probably trying to write this:

SELECT c2.compteur, 
       ( SUM(c1.feb15 + c1.nov15 + c1.dec15) * 100 / 
         (SELECT SUM(c1.feb15 + c1.nov15 + c1.dec15) FROM compteur2015)
       ) 
FROM compteur2015 c1 JOIN
     compteur c2 
     ON c1.compteur = c2.compteur
GROUP BY c2.compteur;

However, this is more simply written as:

SELECT c2.compteur, 
       ( SUM(c1.feb15 + c1.nov15 + c1.dec15) * 100 / 
         SUM(SUM(c1.feb15 + c1.nov15 + c1.dec15)) OVER ()
       ) 
FROM compteur2015 c1 JOIN
     compteur c2 
     ON c1.compteur = c2.compteur
GROUP BY c2.compteur;

Upvotes: 1

adeishs
adeishs

Reputation: 79

Not sure what you’re trying to achieve. You don’t seem to need the subquery.

Try this:

SELECT
    c2.compteur, 
    (c1.feb15 + c1.nov15 + c1.dec15) * 100 / ROUND(c1.feb15 + c1.nov15 + c1.dec15)
FROM compteur2015 c1
    INNER JOIN compteur c2 
        ON c1.compteur = c2.compteur
GROUP BY
    c2.compteur

Upvotes: 0

M Danish
M Danish

Reputation: 478

Try this.

select c2.compteur, ((c1.feb15+c1.nov15+c1.dec15)*100 / (select round(c1.feb15+c1.nov15+c1.dec15) from compteur2015 c15 where c1.compteur = c15.compteur )) 
from compteur2015 c1, compteur c2 
group by c2.compteur

Upvotes: 0

Nitin Zadage
Nitin Zadage

Reputation: 641

Your inner select query select round(c1.feb15+c1.nov15+c1.dec15) from compteur2015 returning more than 1 record. Make sure there is only one valid record returned by your inner query.

Upvotes: 0

Related Questions