Search code examples
sqldb2subquerywhere-clause

How do I add where condition?


enter image description here

I want to add a condition according to the returned result


Solution

  • You can't reuse an alias defined in the SELECT clause in the FROM clause of the scope. You need to either repeat the expression, or use a subquery or CTE.

    select dayofweek_iso(timestamp(mycol)) as mynewcol 
    from mytable
    where dayofweek_iso(timestamp(mycol)) = 1
    

    Or:

    select *
    from (
        select dayofweek_iso(timestamp(mycol)) as mynewcol 
        from mytable
    ) t
    where mynewcol = 1