Is there an easier way to write an SQL query if I want to refer to SELECT subquery in WHERE part of the query?
This is my code:
SELECT
SUM(x) AS test,
(SELECT SUM(y) FROM other_table) AS sub_test,
(test + sub_test) AS total_sum
FROM my_database
WHERE
test <> 0 AND sub_test <> 0
The code is just for illustration :) It works if I put the subquery instead of alias like:
SELECT
SUM(x) AS test,
(SELECT SUM(y) FROM other_table) AS sub_test,
(SUM(x) + (SELECT SUM(y) FROM other_table)) AS total_sum
FROM my_database
WHERE
SUM(x) <> 0 AND (SELECT SUM(y) FROM other_table) <> 0
But writing the whole subquery xx times is a bit awkward since the subquery is not just a plain "SELECT (SUM(..))"... If I want to change something in subquery I have to change it xx times and hope that I changed all of them..