Search code examples
sqloracle-databasesubquerycorrelated-subquery

Select attribute in SQL then checking its value


I am trying to do a SELECT from a table where an attribute BALANCE should be less than 0. The structure is as follows:

SELECT ID_PROJECT FROM PROJECTS WHERE BALANCE < 0;

But BALANCE is a derived attribute, the result of:

SET BALANCE = (SELECT BALANCE FROM(
[...] here goes the 'selection' (which works fine).

The problem is, I don't know how to use the attribute BALANCE selected above in the first SELECT (and I already did a extensive search through google, oracle docs and stack overflow).

Thanks in advance.


Solution

  • You can try with a subquery:

    SELECT ID_PROJECT FROM PROJECTS p1 WHERE (select balance from ...) > 0;
    

    In this way you will probably need to use some condition to link PROJECTS p1 to the table(s) in the nested query, depending on your table structure and your needs