How to eliminate repeating case or decode statement multiple times in SELECT?
For example:
SELECT a,
IF (c = DECODE(b, "foo1", "bar1", "foo2", "bar2") THEN c ELSE 0),
IF (d != DECODE(b, "foo1", "ba开发者_运维知识库r1", "foo2", "bar2") THEN d ELSE 2),
IF (e = DECODE(b, "foo1", "bar1", "foo2", "bar2") THEN e ELSE 5)
FROM x
Seems like there should be a less repetitive way to do this...
use
SELECT
a,
IF (c = b THEN c ELSE 0),
IF (d != b THEN d ELSE 2),
IF (e = b THEN e ELSE 5)
FROM
(SELECT a, DECODE(b, "foo1", "bar1", "foo2", "bar2") b, c, d, e from x);
SELECT a,
DECODE( c, b_decoded, c, 0 ),
DECODE( d, b_decoded, 2, d ),
DECODE( e, b_decoded, e, 5 )
FROM (SELECT a, c, d, e, DECODE(b, "foo1", "bar1", "foo2", "bar2") b_decoded FROM x)
精彩评论