Tjun Tjun - 2 months ago 12
SQL Question

How do I create an if condition using calculated columns in a single table?

My table contains a column called

BATCH_DATE
, and a calculated column that returns the MIN of BATCH_DATE, called
MIN_BATCH_DATE
.

I need a new column
CHECK
that gets a value of A1 when the
BATCH_DATE
is the same as
MIN_BATCH_DATE
, otherwise give it a value of B1.

I was wondering what the best way to tackle this would be?

I want something like this

[BATCH_DATE] [BATCH_DATE_MIN] [CHECK]

2007-05-01 2007-05-01 A1
2007-05-01 2007-02-01 B1

Answer

Try something like:

SELECT (CASE WHEN BATCH_DATE = MIN_BATCH_DATE THEN 'A1'
             ELSE 'B1'
        END)CHECK_DT
Comments