Let's say I have these two columns:
|----------------|----------------|
| Columna1 | Columna2 |
|----------------|----------------|
| 3 | 50 |
| 1 | 150 |
| 2 | 200 |
| 3 | 750 |
| 1 | 180 |
|----------------|----------------|
For the maximum value of column1 I want to sum the values of column2.
That is, first I want to select the largest value from Column1 (let's say it's called maxvalue)
Next, I want to take all the values in column2 where column1=maxvalue, and add them together.
- In this case the maximum of column 1 is 3
- The values of column 2 whose column1 is equal to 3 are 50 and 750
- I try to make a query that, in this case, would give me 800.
I have tried to do the following:
SELECT SUM(Columna2) FROM tabla where MAX(Columna1) GROUP BY Columna1
.. that gives me this error:
PDOException: SQLSTATE[HY000]: General error: 1111 Invalid use of group function
I think you are missing, it would be something like this:
in the subquery you look for which is the largest column1 and then you only compare if the column1 that you are the largest
What happens is that you cannot make a
WHERE MAX(Columna1)
, you must make a subquery in the following way, we select theColumna2
and add it, then insideWHERE
we make the subquery to bring theMAX
of theColumna1
,The previous query will give you a more or less similar result: