How I can replicate the value of a exact field on the same Select statement?
What I have:
SELECT
FIELD1,
FIELD2,
CASE WHEN FIELD3 <> FIELD4
THEN CASE WHEN SUBSTRING(FIELD10,1,3) = FIELD5
THEN (FIELD1 * FIELD3) + FIELD4
ELSE (FIELD1 / FIELD3) + FIELD4
END
END AS CUSTOM_FIELD1,
-- does the same CASE only to set it to another field.
-- i don't want to make the same case again, I want it to be smarter and use the same value as CUSTOM_FIELD1 without needing to calculate again.
CASE WHEN FIELD3 <> FIELD4
THEN CASE WHEN SUBSTRING(FIELD10,1,3) = FIELD5
THEN (FIELD1 * FIELD3) + FIELD4
ELSE (FIELD1 / FIELD3) + FIELD4
END
END AS CUSTOM_FIELD2
I bet there is a way to make this code smarter and cleaner, I just don't know how.