今天的序列号

时间:2016-09-09 17:32:42

标签: sql oracle

我有一个表记录从我们的Web服务发送的事务。我希望获得当天表格中一个数字和最后一个数字之间的序列号。

这是一个简化的结构: 表名CARTREQUESTS

CR_ID
CR_DATE
CR_ORDER_ID
CR_CUSTOMERID

CR_ID   CR_DATE                 CR_ORDER_ID CR_CUSTOMERID
1540668 9/9/2016 11:59:52 AM    100148466   1087840
1540667 9/9/2016 11:58:49 AM    100148465   1191293
1540687 9/9/2016 12:23:07 PM    100148464   916470
1540647 9/9/2016 11:46:32 AM    100148463   918195
1540629 9/9/2016 11:45:08 AM    100148462   957676
1540628 9/9/2016 11:44:28 AM    100148461   1161585
1540627 9/9/2016 11:43:48 AM    100148460   956791
1540607 9/9/2016 11:40:47 AM    100148459   1161585
1540567 9/9/2016 11:36:07 AM    100148457   1127193
1540548 9/9/2016 11:30:42 AM    100148456   1204204
1540547 9/9/2016 11:29:11 AM    100148455   967946
1540527 9/9/2016 11:27:25 AM    100148454   1209116
1540507 9/9/2016 11:21:54 AM    100148453   1207376
1540488 9/9/2016 11:18:17 AM    100148452   1057770
1540467 9/9/2016 11:02:16 AM    100148450   1216001
1540448 9/9/2016 11:00:51 AM    100148449   1082302
1540447 9/9/2016 11:00:39 AM    100148448   1082298
1540427 9/9/2016 10:58:10 AM    100148447   951007
1540407 9/9/2016 10:56:10 AM    100148446   1209338
1540387 9/9/2016 10:51:08 AM    100148445   1205880
1540367 9/9/2016 10:39:12 AM    100148444   1061172
1540347 9/9/2016 10:34:37 AM    100148443   1215973

我需要知道如何输入CR_ORDERID值100148443并且查询返回这2个顺序#s 100148458,100148451

此时有CR_ORDERID值大于100148466,我只想要处理SYSDATE-1的记录

2 个答案:

答案 0 :(得分:0)

您引用的值在列表中不存在,因此我假设您正在查找昨天不存在的订单。如果是这样,试试这个:

start_order := 100148443;
select max(cr_order_id) into end_order from cartrequests where cr_order_id >= start_order and cr_date between trunc(sysdate) - 1 and trunc(sysdate);
for i in start_order..end_order loop
  select count(*) into ocount from cartrequests where cr_order_id = i and cr_date between trunc(sysdate)-1 and trunc(sysdate)
  if ocount = 0 then
    dbms.output.put_line (i);
  end if;
end loop;

答案 1 :(得分:0)

最终工作代码:

select minid+(level-1) missingorders from (
    select max(cr_order_id) maxid, min(cr_order_id) minid
    from CARTREQUESTS
    WHERE cr_date >= sysdate - 1 
     and cr_order_id >= :orderid
)
connect by level <= maxid-minid+1
minus
select cr_order_id from CARTREQUESTS
WHERE cr_date >= sysdate - 1