Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Como comparar valores numéricos nas mesmas colunas para várias colunas usando Oracle SQL

Create Table V(TName VARCHAR(5), Oct_14 NUMBER(5,2), Nov_14 NUMBER(5,2), Dec_14 NUMBER(5,2));

Insert into V values('T1', 1.2, 1.2, 1.2);
Insert into V values('T2', 1.4, 1.5, 1.6);
Insert into V values('T3', 1.5, 1.5, 1.7);

SELECT TName, To_Char(Oct_14) Oct_14, To_Char(Nov_14) Nov_14, To_Char(Dec_14) Dec_14 from V
UNION
SELECT 'Compare', 
(select 'ATTN' from dual where EXISTS (select 1 from V where TName = 'T3' and OCT_14 > ALL(SELECT OCT_14 FROM V WHERE TName IN ('T1','T2')))) Oct_14_Res,
(select 'ATTN' from dual where EXISTS (select 1 from V where TName = 'T3' and Nov_14 > ALL(SELECT Nov_14 FROM V WHERE TName IN ('T1','T2')))) Nov_14_Res,
(select 'ATTN' from dual where EXISTS (select 1 from V where TName = 'T3' and Dec_14 > ALL(SELECT Dec_14 FROM V WHERE TName IN ('T1','T2')))) Dec_14_Res
FROM dual;

Deve haver outras maneiras de fazê-lo melhor.