之前选择数据&午夜后

时间:2016-10-06 08:41:32

标签: sql oracle

我有一个仪表板,可以报告班次每小时的生产情况。仪表板每班次都有固定的插槽。

  • 当班次是早上7点到晚上7点时,一切都很好。
  • 当班次是晚上7点到早上7点时,我有一个问题。
  • 晚上7点到晚上8点很好
  • 晚上8点到晚上9点很好
  • 晚上9点到晚上10点很好
  • 晚上10点到晚上11点很好
  • 晚上11点到中午12点很好
  • 中午12点到凌晨1点是一个问题 - 问题是当你在晚上7点登录仪表板时,我会从午夜到凌晨1点拿到今天制作的作品。 我如何编码我的sql来忽略午夜前的午夜到凌晨1点的数据,并在午夜之后选择它?

这是我正在使用的SQL

select date_time_created,
       ith.quantity
From   ifsapp.inventory_transaction_hist2 ith
where  TRANSACTION_CODE = 'OOREC'
and    upper(ifsapp.Inventory_Part_API.Get_Unit_Meas('WW001',PART_NO) ) =upper( 'CS' )
and    date_time_created -1 - 60*60*7/86400 >= to_date(to_char(sysdate -1,'DD/MM/YYYY') || ' 19:00:00','DD/MM/YYYY HH24:MI:SS')- 60*60*7/86400
and    date_time_created - 60*60*7/86400 <= to_date(to_char(sysdate,'DD/MM/YYYY') || ' 19:59:59','DD/MM/YYYY HH24:MI:SS')- 60*60*7/86400
and    userid='3520'

感谢

2 个答案:

答案 0 :(得分:0)

晚上7点到早上7点:

date_time_created between to_date( to_char(sysdate-1, 'ddmmyyyy') || '19:00:00', 'ddmmyyhh24:mi:ss')
                    AND   to_date( to_char(sysdate, 'ddmmyyyy')   || '07:00:00', 'ddmmyyhh24:mi:ss')

中午12点至凌晨1点:

date_time_created between to_date( to_char(sysdate-1, 'ddmmyyyy') || '12:00:00', 'ddmmyyhh24:mi:ss')
                    AND   to_date( to_char(sysdate, 'ddmmyyyy')   || '01:00:00', 'ddmmyyhh24:mi:ss')

答案 1 :(得分:0)

我不完全确定您之后的情况,但如果您想要查看当前班次的结果,希望此示例可以帮助您找出您需要做的事情:

WITH dates AS (SELECT TRUNC(SYSDATE) + 5.1/24 + LEVEL/24 dt
               FROM   dual
               CONNECT BY LEVEL <= 36)
-- end of generating some data for the following query to run against
SELECT dt,
       CASE WHEN to_number(to_char(dt - 7/24, 'hh24')) < 12 THEN '7am-7pm'
            ELSE '7pm-7am'
       END shift
FROM   dates
WHERE  dt >= CASE WHEN to_number(to_char(SYSDATE - 7/24, 'hh24')) < 12 THEN TRUNC(SYSDATE -7/24) + 7/24
                  ELSE TRUNC(SYSDATE -7/24) + 19/24
             END
AND    dt < CASE WHEN to_number(to_char(SYSDATE - 7/24, 'hh24')) < 12 THEN TRUNC(SYSDATE -7/24) + 19/24
                 ELSE TRUNC(SYSDATE -7/24) + 31/24
            END;

DT                  SHIFT
------------------- -------
06/10/2016 07:06:00 7am-7pm
06/10/2016 08:06:00 7am-7pm
06/10/2016 09:06:00 7am-7pm
06/10/2016 10:06:00 7am-7pm
06/10/2016 11:06:00 7am-7pm
06/10/2016 12:06:00 7am-7pm
06/10/2016 13:06:00 7am-7pm
06/10/2016 14:06:00 7am-7pm
06/10/2016 15:06:00 7am-7pm
06/10/2016 16:06:00 7am-7pm
06/10/2016 17:06:00 7am-7pm
06/10/2016 18:06:00 7am-7pm

为了证明对于不同的sysdates,报告会生成,我已经设置了查询以使用不同的&#34; sysdts&#34;模仿在白天的不同时间运行:

WITH dates AS (SELECT TRUNC(SYSDATE) + 5.1/24 + LEVEL/24 dt
               FROM   dual
               CONNECT BY LEVEL <= 36),
-- end of generating some data for the main query to run against
 sys_dates AS (SELECT to_date('06/10/2016 20:10:23', 'dd/mm/yyyy hh24:mi:ss') sysdt FROM dual UNION ALL
               SELECT to_date('06/10/2016 13:10:23', 'dd/mm/yyyy hh24:mi:ss') sysdt FROM dual UNION ALL
               SELECT to_date('07/10/2016 01:10:23', 'dd/mm/yyyy hh24:mi:ss') sysdt FROM dual)
-- end of mimicking different sysdates that the report could be run against
-- - for testing purposes only
SELECT sd.sysdt run_time_of_report,
       d.dt,
       CASE WHEN to_number(to_char(d.dt - 7/24, 'hh24')) < 12 THEN '7am-7pm'
            ELSE '7pm-7am'
       END shift
FROM   dates d
       CROSS JOIN sys_dates sd
WHERE  d.dt >= CASE WHEN to_number(to_char(sd.sysdt - 7/24, 'hh24')) < 12 THEN TRUNC(sd.sysdt -7/24) + 7/24
                    ELSE TRUNC(sd.sysdt -7/24) + 19/24
               END
AND    d.dt < CASE WHEN to_number(to_char(sd.sysdt - 7/24, 'hh24')) < 12 THEN TRUNC(sd.sysdt -7/24) + 19/24
                   ELSE TRUNC(sd.sysdt -7/24) + 31/24
              END
ORDER BY sd.sysdt, d.dt;

RUN_TIME_OF_REPORT  DT                  SHIFT
------------------- ------------------- -------
06/10/2016 13:10:23 06/10/2016 07:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 08:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 09:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 10:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 11:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 12:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 13:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 14:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 15:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 16:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 17:06:00 7am-7pm
06/10/2016 13:10:23 06/10/2016 18:06:00 7am-7pm
--
06/10/2016 20:10:23 06/10/2016 19:06:00 7pm-7am
06/10/2016 20:10:23 06/10/2016 20:06:00 7pm-7am
06/10/2016 20:10:23 06/10/2016 21:06:00 7pm-7am
06/10/2016 20:10:23 06/10/2016 22:06:00 7pm-7am
06/10/2016 20:10:23 06/10/2016 23:06:00 7pm-7am
06/10/2016 20:10:23 07/10/2016 00:06:00 7pm-7am
06/10/2016 20:10:23 07/10/2016 01:06:00 7pm-7am
06/10/2016 20:10:23 07/10/2016 02:06:00 7pm-7am
06/10/2016 20:10:23 07/10/2016 03:06:00 7pm-7am
06/10/2016 20:10:23 07/10/2016 04:06:00 7pm-7am
06/10/2016 20:10:23 07/10/2016 05:06:00 7pm-7am
06/10/2016 20:10:23 07/10/2016 06:06:00 7pm-7am
-- 
07/10/2016 01:10:23 06/10/2016 19:06:00 7pm-7am
07/10/2016 01:10:23 06/10/2016 20:06:00 7pm-7am
07/10/2016 01:10:23 06/10/2016 21:06:00 7pm-7am
07/10/2016 01:10:23 06/10/2016 22:06:00 7pm-7am
07/10/2016 01:10:23 06/10/2016 23:06:00 7pm-7am
07/10/2016 01:10:23 07/10/2016 00:06:00 7pm-7am
07/10/2016 01:10:23 07/10/2016 01:06:00 7pm-7am
07/10/2016 01:10:23 07/10/2016 02:06:00 7pm-7am
07/10/2016 01:10:23 07/10/2016 03:06:00 7pm-7am
07/10/2016 01:10:23 07/10/2016 04:06:00 7pm-7am
07/10/2016 01:10:23 07/10/2016 05:06:00 7pm-7am
07/10/2016 01:10:23 07/10/2016 06:06:00 7pm-7am

您可以看到截至2016年6月6日20:10:23和07/10/2016 01:10:23的报告会在晚上7点到7点之间撤回相同的数据。