如何使用sqldeveloper中的IF else条件检索最新实例

时间:2018-01-09 10:25:26

标签: sql oracle oracle10g

我有一张学生表,如下所示

ROLL  |  STUDENT | SUBJECT | VERSION_ID |

  1        A         M         1
  2        B         M         2 
  3        C         M         3
  4        B         S         1
  5        D         S         2
  6        E         E         1 
  7        F         G         1 

如果SUBJECT ='M'至少有一条记录,则根据版本ID检索SUBJECT'M'的最新记录。
ELSIF如果SUBJECT ='S'至少有一条记录,则根据版本ID检索SUBJECT'S'的最新记录。
ELSIF如果SUBJECT ='E'至少有一条记录,则根据版本ID检索SUBJECT'E'的最新记录。
否则不要取任何记录。

基于以上条件,我试图编写一个可以满足上述所有条件的查询或视图,并给出以下输出。请告诉我可能的解决方案。

输出

ROLL  |  STUDENT | SUBJECT | VERSION_ID | 
  3        C         M         3

我尝试使用以下查询获得所需的输出,但我遇到了错误。请帮助我以更好的性能解决此查询。

SELECT * from ( IF EXISTS(select subject from STUDENT where SUBJECT='M') then
select * from STUDENT S1
inner join
(select S2.ROLL from STUDENT S2
      where 
          S2.ROLL=(select S3.ROLL from STUDENT S3
                       where S3.SUBJECT='M'
                   and S3.VERSION_ID=(select MAX(S4.VERSION_ID) from STUDENT S4
                           GROUP BY S4.SUBJECT
                           HAVING S4.SUBJECT='M'))) S5
ON
S1.ROLL=S5.ROLL
ELSIF EXISTS(select subject from STUDENT where SUBJECT='S') then
select * from STUDENT S1
inner join
(select S2.ROLL from STUDENT S2
      where 
          S2.ROLL=(select S3.ROLL from STUDENT S3
                       where S3.SUBJECT='S'
                   and S3.VERSION_ID=(select MAX(S4.VERSION_ID) from STUDENT S4
                           GROUP BY S4.SUBJECT
                           HAVING S4.SUBJECT='S'))) S5
           ON
          S1.ROLL=S5.ROLL
ELSIF EXISTS(select subject from STUDENT where SUBJECT='E') then
select * from STUDENT S1
inner join
(select S2.ROLL from STUDENT S2
      where 
          S2.ROLL=(select S3.ROLL from STUDENT S3
                       where S3.SUBJECT='E'
                   and S3.VERSION_ID=(select MAX(S4.VERSION_ID) from STUDENT S4
                           GROUP BY S4.SUBJECT
                           HAVING S4.SUBJECT='E'))) S5
           ON
          S1.ROLL=S5.ROLL
ELSE 
  SELECT * FROM STUDENT WHERE 1=2);

TABLE CREATION:

Create table student (ROLL number,STUDENT varchar2(20),SUBJECT varchar2(20),VERSION_ID number );  

插入数据:

insert into STUDENT values(1,'A','M',1);
insert into STUDENT values(2,'B','M',2);
insert into STUDENT values(3,'C','M',3);
insert into STUDENT values(4,'B','S',1);
insert into STUDENT values(5,'D','S',2);
insert into STUDENT values(6,'E','E',1);
insert into STUDENT values(7,'F','G',1);  

谢谢..

2 个答案:

答案 0 :(得分:3)

我们可以使用您在问题中提供的排序逻辑将ROW_NUMBER应用于整个表格。主题MSE按此顺序优先,VERSION_ID打破平局。主题与这三者不匹配的行被赋予最低优先级。如果没有匹配主题的记录,我们会过滤掉WHERE子句中的所有记录。

WITH cte AS (
    SELECT ROLL, STUDENT, SUBJECT, VERSION_ID,
        ROW_NUMBER() OVER (ORDER BY CASE WHEN SUBJECT = 'M' THEN 1
                                         WHEN SUBJECT = 'S' THEN 2
                                         WHEN SUBJECT = 'E' THEN 3
                                         ELSE 4 END, VERSION_ID DESC END) rn
)

SELECT ROLL, STUDENT, SUBJECT, VERSION_ID
FROM cte
WHERE rn = 1 AND SUBJECT IN ('M', 'S', 'E')

答案 1 :(得分:0)

你想要正确答案吗?试试这个:

SELECT TOP 1 #student.*
FROM   student
JOIN   (

SELECT  MAX(VERSION_ID) VERSION_ID,SUBJECT
FROM   student
WHERE  SUBJECT = 'M'
GROUP BY SUBJECT

UNION ALL 

SELECT  MAX(VERSION_ID),SUBJECT
FROM   student
WHERE  SUBJECT = 'S'
GROUP BY SUBJECT

UNION ALL

SELECT  MAX(VERSION_ID),SUBJECT
FROM   student
WHERE  SUBJECT = 'E'
GROUP BY SUBJECT

UNION ALL 

SELECT  MAX(VERSION_ID),SUBJECT
FROM   student
WHERE  SUBJECT = 'G'
GROUP BY SUBJECT

UNION ALL

SELECT NULL,NULL

)       AS Dtls
ON     (student.SUBJECT     =      Dtls.SUBJECT
AND student.VERSION_ID     =      Dtls.VERSION_ID)