0

I have this table:

|car |house|
|2010|2012 |
|2005|2004 |
|2003|2003 |
|2006| |

and I need to create this table from it:

|car |house|result|
|2010|2012 |after |
|2005|2004 |before|
|2003|2003 |after |
|2006| |x |

cases:
car<house result=before
car>house result=after
car=house result=after
house is empty -> result=x

1 Answer 1

3

Use CASE EXPRESSION :

SELECT t.car,t.house,
       CASE WHEN t.house >= t.car THEN 'AFTER'
            WHEN t.house < t.car THEN 'BEFORE'
            ELSE 'x'
       END as result
FROM YourTable t

EDIT: To join this to another table :

SELECT * FROM (
    SELECT t.car,t.house,
           CASE WHEN t.house >= t.car THEN 'AFTER'
                WHEN t.house < t.car THEN 'BEFORE'
                ELSE 'x'
           END as result
    FROM YourTable t) s
JOIN Another_Table tt
 ON(s.result = tt.Column)
Sign up to request clarification or add additional context in comments.

1 Comment

with this code I get only data output with this new column(result), but how can I join this column in to t. table ? thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.