I have a problem with an SQL query.
SELECT SUM(table_colum) AS value, SUM(value * 3) AS value2 FROM table;
You need to know this is a short representation of my whole query.
The error:
Unknown column 'value' in 'field list'
Is there a way to reuse value
in another SUM()
?
You can just do:
SELECT SUM(table_colum) AS value, SUM(SUM(table_colum) * 3) AS value2 FROM table;
Internally, the server will only do the SUM(table_colum)
calculation once and use the result twice.
I suppose you could write
SELECT value, SUM(value * 3) AS value2
FROM ( SELECT SUM(table_column) AS value
FROM table
) AS t
;
But as I mentioned in a comment above, I'm not sure what you would want this for. SUM(table_column)
is just a single value, so the SUM
of it is just the same value. So you'd get the same result by writing:
SELECT value, value * 3 AS value2
FROM ( SELECT SUM(table_column) AS value
FROM table
) AS t
;
without the second SUM
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With