使用2个条件对Oracle表中的数据进行排序

时间:2014-03-14 03:44:17

标签: sql oracle

我有一个名为jobs的Oracle表。在这张表中,我有2个工作,JOB 1和JOB 2.我需要根据以下条件获得一份工作。

  1. IS_ASSIST JOB_PROCESS必须有JOB_PROCESS_STATE = '0'JOB 1和2都满足了这一点。
  2. 如果两个工作都是' IS_ASSIST的状态为0,然后获得具有JOB_PROCESS = IS_PULLAHEADJOB_PROCESS_STATE = 1的作业。
  3. 这应该是ID = 4的最后一行。我如何在查询中写下这些条件?这是一个仅应用第一个条件的示例。我怎么也应用第二个条件?

    SELECT JOB_ID 
    FROM JOB 
    WHERE JOB_PROCESS = 'IS_ASSIST' 
    AND JOB_PROCESS_STATE = '0'
    

    工作表:

    +-------------------------------------------+
    |ID |JOB_ID|  JOB_PROCESS |JOB_PROCESS_STATE|
    +-------------------------------------------+
    | 1 |  1   |   IS_ASSIST  |      0          |
    |-------------------------------------------|
    | 2 |  1   | IS_PULLAHEAD |      0          |
    |-------------------------------------------|
    | 3 |  2   |   IS_ASSIST  |      0          |
    |-------------------------------------------|
    | 4 |  2   | IS_PULLAHEAD |      1          |
    +-------------------------------------------+
    

    提前谢谢!

1 个答案:

答案 0 :(得分:1)

从此查询中获取第一条记录

select JOB_ID
from JOB
where JOB_ID IN
(
  SELECT JOB_ID 
  FROM JOB
  WHERE JOB_PROCESS = 'IS_ASSIST' AND JOB_PROCESS_STATE = '0'
  GROUP BY JOB_ID
)
ORDER BY case when JOB_PROCESS = IS_PULLAHEAD AND JOB_PROCESS_STATE = 1 
              then 0
              else 1 
         end