ORACLE:如果列的最新值等于X

时间:2015-12-03 18:12:54

标签: oracle select max

我先发制人地说我在网上搜索其他问题,但找不到。这对我来说是否是不好的措辞选择,或者其他。

我有一个票务队列并搜索它的历史记录。专栏" Group"将包含在转移操作时拥有该票证的组名称。还有许多其他操作,例如只是向故障单添加注释。我只对列中的最新值(忽略空值)感兴趣,如果它是" QUEUE_X"。

我知道这是不合适的,因为组功能,但这在逻辑上我是如何看待它的:

select
    * 
from 
    ticket_history
where
    max(group) = 'QUEUE_X'
order by
    date
;

我不想严格执行where group = 'X',因为门票可以转移很多,我只关心当前队列中的门票。此外,我无法找到原始开发人员是否或在哪里放置类似CURRENT_QUEUE的列,所以运气好的方法很简单。不要太过字面化,只是想说明GROUP中的空值,我想最近的值等于Queue_X

想要的票证的示例表数据(但如果查询发生在12:12,我会有):

STRT_DATE     | PERSON | GROUP   | TICKET_NUM | END_DATE
12/3/15 12:00 | Ted    | Queue_Y | 111111     | 12/3/15 12:05
12/3/15 12:05 | Bob    |         | 111111     | 12/3/15 12:10
12/3/15 12:10 | Ted    | Queue_Y | 222222     | 12/3/15 12:45
12/3/15 12:10 | Bob    | Queue_X | 111111     | 12/3/15 12:15
12/3/15 12:15 | Bob    |         | 222222     | 12/3/15 12:45
12/3/15 12:15 | Mary   | Queue_Y | 111111     | 12/3/15 12:25
12/3/15 12:25 | Ted    |         | 222222     | 12/3/15 12:35
12/3/15 12:35 | Ted    |         | 333333     | 12/3/15 12:45
12/3/15 12:35 | Bob    | Queue_X | 333333     | 12/3/15 12:45
12/3/15 12:45 | Ted    |         | 111111     | 12/3/15 12:55
12/3/15 12:55 | Bob    | Queue_X | 111111     | 12/3/15 12:56

期望的结果:

12/3/15 12:35 | Bob    | Queue_X | 333333     | 12/3/15 12:45
12/3/15 12:55 | Bob    | Queue_X | 111111     | 12/3/15 12:56

(仍在Queue_Y中):

12/3/15 12:10 | Ted    | Queue_Y | 222222     | 12/3/15 12:45

提前谢谢。

编辑:为Queue_Y添加了更多票证和一个额外成员(因此队列与人之间没有1-1关系)

3 个答案:

答案 0 :(得分:0)

试试这个(已编辑):

select strt_date, person, group, ticket_num, end_date
from ticket_history
where (rowid, end_date) IN (select rowid, max(end_date)
                           from ticket_history
                           where group = 'QUEUE_X'
                           group by rowid)
order by end_date;

这将反映您在所需输出中显示的内容。

答案 1 :(得分:0)

不完全确定你想要完成什么,但我认为你想要像dense_rank()这样的东西我把它放在一起mytable代表你的数据,t1做密集等级检查

这里是12:15之后运行的查询,没有返回数据。

WITH mytable
       AS (SELECT   TO_DATE ('12/3/15 12:00', 'mm/dd/yy hh:mi') AS strt_dt,
                    'Ted' AS person,
                    'Queue_Y' AS grp,
                    123456 AS ticket_num
             FROM   DUAL
           UNION ALL
           SELECT   TO_DATE ('12/3/15 12:05', 'mm/dd/yy hh:mi') AS strt_dt,
                    'Bob',
                    NULL,
                    123456
             FROM   DUAL
           UNION ALL
           SELECT   TO_DATE ('12/3/15 12:10', 'mm/dd/yy hh:mi') AS strt_dt,
                    'Bob',
                    'Queue_X',
                    123456
             FROM   DUAL
           UNION ALL
           SELECT   TO_DATE ('12/3/15 12:15', 'mm/dd/yy hh:mi') AS strt_dt,
                    'Ted',
                    'Queue_Y',
                    123456
             FROM   DUAL),
    t2
       AS (SELECT   mytable.*,
                    DENSE_RANK ()
                       OVER (PARTITION BY ticket_num ORDER BY strt_dt DESC)
                       AS test_for_Q
             FROM   mytable)
SELECT   strt_dt, person, grp, ticket_num
  FROM   t2
 WHERE   ticket_num IN (SELECT   ticket_num
                          FROM   t2
                         WHERE   test_for_Q = 1 AND Grp = 'Queue_X')

现在这里是在12:12运行的查询,您将获得数据。

WITH mytable
       AS (SELECT   TO_DATE ('12/3/15 12:00', 'mm/dd/yy hh:mi') AS strt_dt,
                    'Ted' AS person,
                    'Queue_Y' AS grp,
                    123456 AS ticket_num
             FROM   DUAL
           UNION ALL
           SELECT   TO_DATE ('12/3/15 12:05', 'mm/dd/yy hh:mi') AS strt_dt,
                    'Bob',
                    NULL,
                    123456
             FROM   DUAL
           UNION ALL
           SELECT   TO_DATE ('12/3/15 12:10', 'mm/dd/yy hh:mi') AS strt_dt,
                    'Bob',
                    'Queue_X',
                    123456
             FROM   DUAL
         --  UNION ALL
        --   SELECT   TO_DATE ('12/3/15 12:15', 'mm/dd/yy hh:mi') AS strt_dt,
              --      'Ted',
              --      'Queue_Y',
              --      123456
           --  FROM   DUAL
           ) ,
    t2
       AS (SELECT   mytable.*,
                    DENSE_RANK ()
                       OVER (PARTITION BY ticket_num ORDER BY strt_dt DESC)
                       AS test_for_Q
             FROM   mytable)
SELECT   strt_dt, person, grp, ticket_num
  FROM   t2
 WHERE   ticket_num IN (SELECT   ticket_num
                          FROM   t2
                         WHERE   test_for_Q = 1 AND Grp = 'Queue_X')

结果

STRT_DT,PERSON,GRP,TICKET_NUM
12/3/2015 12:10:00 PM,Bob,Queue_X,123456
12/3/2015 12:05:00 PM,Bob,,123456
12/3/2015 12:00:00 PM,Ted,Queue_Y,123456

答案 2 :(得分:0)

如果我理解您的问题,您的查询将是:

select a.* 
  from ticket_history a
           INNER JOIN ( select PERSON, GROUP, max(STRT_DATE) as maxdt
                          from ticket_history 
                         where group = 'Queue_X'
                         group by PERSON, GROUP ) b
                   ON (    a.person = b.person 
                       and a.group = b.group
                       and a.STRT_DATE = b.maxdt )
 order by a.STRT_DATE desc

请查看它是否是您需要的,否则请告诉我。