In this SQL query:
select
name,
age,
(select sum(something) from sometable where sometable.code = people.code)
from people
where THIRD_COLUMN > 0
How to reference the result of the third column without having to repeat the SQL?
In this SQL query:
select
name,
age,
(select sum(something) from sometable where sometable.code = people.code)
from people
where THIRD_COLUMN > 0
How to reference the result of the third column without having to repeat the SQL?
You can't. You can use a subquery and give the column a name, though:
select p.name, p.age, p.THIRD_COLUMN
from (select name, age,
(select sum(something) from sometable where sometable.code = people.code
) as THIRD_COLUMN
from people
) p
where THIRD_COLUMN > 0;
I dont know which dms you use but in mysql you can use:
select
name,
age,
(select sum(something) from sometable where sometable.code = people.code) sum_people
from people
having sum_people > 0