简体   繁体   English

获取下一个大于值的日期时间

[英]Get the next Datetime thats greater than value

Get the next available time from a table for the same Type thats greater than the pickuptime available for the type.从表中获取相同类型的下一个可用时间,该时间大于该类型的可用取货时间。 I tried using the FIRST_VALUE function but that doesn't seem to work well.我尝试使用 FIRST_VALUE 函数,但似乎效果不佳。

DROP TABLE IF EXISTS Test1;
CREATE TEMPORARY TABLE Test1
(
    Type        VARCHAR(50),
    Pickup_Time TIMESTAMP,
    INSERT_TIME      TIMESTAMP
);
DROP TABLE IF EXISTS Test2;
CREATE TEMPORARY TABLE Test2
(
    Type VARCHAR(50),
    TIMEFRAME  TIMESTAMP
);


INSERT INTO Test1
VALUES ('MAN', '2020-11-01 09:00:00.000000', '2020-11-01 06:00:00.000000');
INSERT INTO Test1
VALUES ('CAT', '2020-11-02 11:00:00.000000', '2020-11-02 05:00:00.000000');


INSERT INTO Test2
VALUES ('MAN', '2020-11-01 01:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 02:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 03:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 04:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 05:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 06:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 07:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 08:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 09:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 10:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 11:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 12:00:00.000000');
INSERT INTO Test2
VALUES ('MAN', '2020-11-01 13:00:00.000000');


INSERT INTO Test2
VALUES ('CAT', '2020-11-01 01:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-01 02:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-01 03:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-01 04:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-01 05:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-02 06:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-02 07:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-02 08:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-02 09:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-02 10:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-02 11:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-02 12:00:00.000000');
INSERT INTO Test2
VALUES ('CAT', '2020-11-02 13:00:00.000000');


SELECT DISTINCT TT.*,
                FIRST_VALUE(CT.TIMEFRAME)
                OVER (PARTITION BY TT.Type ORDER BY TIMEFRAME rows between unbounded preceding and unbounded following)
FROM Test1 TT
         INNER JOIN Test2 CT on TT.Type = CT.Type
    AND TT.Pickup_Time < CT.TIMEFRAME;

The following query provides me the required output as desired以下查询为我提供了所需的输出

+----+--------------------------+--------------------------+--------------------------+
|type|pickup_time               |insert_time               |first_value               |
+----+--------------------------+--------------------------+--------------------------+
|MAN |2020-11-01 09:00:00.000000|2020-11-01 06:00:00.000000|2020-11-01 10:00:00.000000|
|CAT |2020-11-02 11:00:00.000000|2020-11-02 05:00:00.000000|2020-11-02 12:00:00.000000|
+----+--------------------------+--------------------------+--------------------------+

But when I do another insert for the type MAN with a different time and try to run the same SQL statement it doesn't meet my criteria但是当我用不同的时间为类型 MAN 做另一个插入并尝试运行相同的 SQL 语句时,它不符合我的标准

INSERT INTO Test1
VALUES ('MAN', '2020-11-01 03:00:00.000000', '2020-11-01 01:00:00.000000');



+----+--------------------------+--------------------------+--------------------------+
|type|pickup_time               |insert_time               |first_value               |
+----+--------------------------+--------------------------+--------------------------+
|MAN |2020-11-01 03:00:00.000000|2020-11-01 01:00:00.000000|2020-11-01 04:00:00.000000|
|MAN |2020-11-01 09:00:00.000000|2020-11-01 06:00:00.000000|2020-11-01 04:00:00.000000|
|CAT |2020-11-02 11:00:00.000000|2020-11-02 05:00:00.000000|2020-11-02 12:00:00.000000|
+----+--------------------------+--------------------------+--------------------------+

Instead of 2020-11-01 04:00:00.000000 the value should have been 2020-11-01 10:00:00.000000 for the second MAN entry above.对于上面的第二个 MAN 条目,值应该是2020-11-01 10:00:00.000000而不是2020-11-01 04:00:00.000000

Is First_Value the right option to be used here if yes how to derive the logic if or which is the best function that can be used to achieve this. First_Value 是否是此处使用的正确选项,如果是,如何导出逻辑,如果或哪个是可用于实现此目的的最佳函数。

The error happens because you have 2 entries for MAN but you just partition by type and therefore both entries are handled the same, you need to partition by Pickup_Time or ÌNSERT_TIME :发生错误是因为您有 2 个MAN条目,但您只是按type分区,因此两个条目的处理方式相同,您需要按Pickup_TimeÌNSERT_TIME进行分区:

FIRST_VALUE(CT.TIMEFRAME)
                OVER (PARTITION BY TT.Type,TT.Pickup_Time ORDER BY TIMEFRAME rows between unbounded preceding and unbounded following)

This will generate:这将生成:


type    pickup_time insert_time first_value
MAN 2020-11-01 03:00:00 2020-11-01 01:00:00 2020-11-01 04:00:00
CAT 2020-11-02 11:00:00 2020-11-02 05:00:00 2020-11-02 12:00:00
MAN 2020-11-01 09:00:00 2020-11-01 06:00:00 2020-11-01 10:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM