Search code examples
sqlderbycolumn-alias

Derby SQL calculation


I'm having a trouble to do a calculation in Derby.

The problem is the next:

Select column1, (column1 + 10) as newCol, 
(column1+newCol) as newCol2 from sometable;

This throws an error saying that newCol does not exist, in the case (column1+newCol).

Why is not that correct?

Thanks for your help! 😁


Solution

  • You can't access a column alias on the same level where you define it.

    You need a derived table:

    select column1, newcol, column1 + newcol as newcol2
    from (
      Select column1, (column1 + 10) as newCol
      from sometable
    ) t;