繁体   English   中英

在配置单元中选择 minValue 及其行

[英]Picking minValue and its row in hive

我必须在 2 小时的滑动日期窗口及其相应的日期值上选择 minValue。 例如

Create table stock(time string, cost float);

Insert into stock values("1990-01-01 8:00 AM",4.5);
Insert into stock values("1990-01-01 9:00 AM",3.2);
Insert into stock values("1990-01-01 10:00 AM",3.1);
Insert into stock values("1990-01-01 11:00 AM",5.5);
Insert into stock values("1990-01-02 8:00 AM",5.1);
Insert into stock values("1990-01-02 9:00 AM",2.2);
Insert into stock values("1990-01-02 10:00 AM",1.5);
Insert into stock values("1990-01-02 11:00 AM",6.5);
Insert into stock values("1990-01-03 8:00 AM",8.1);
Insert into stock values("1990-01-03 9:00 AM",3.2);
Insert into stock values("1990-01-03 10:00 AM",2.5);
Insert into stock values("1990-01-03 11:00 AM",4.5);

为此,我可以编写这样的查询

select min(cost) over(order by unix_timestamp(time) range between current row and 7200 following)
from stock

因此,从当前行向前看 2 小时(7200 秒)并选择最小值,因此第一行的最小值将为 3.1,位于第三行的上午 10:00 我通过此查询获得了正确的最小值,但我还需要最小值的相应日期值,在这种情况下,我想要“1990-01-01 10:00 AM”。 我该如何选择这个?

谢谢,拉吉

我认为这是一个难题。 一种方法是join以查找值:

select s.*
from (select s.*,
             min(cost) over (order by unix_timestamp(time) range between current row and 7200 following) as min_cost,
      from stock s
     ) s join
     stock smin
     on smin.cost = min_cost and
        unix_timestamp(smin.time) >= unix_timestamp(s.time) and
        unix_timestamp(smin.time) < unix_timestamp(s.time) + 7200

这种方法的缺点是它可能会产生重复项。 如果这是一个问题:

select s.*
from (select s.*, smin.time as min_time,
             row_number() over (partition by s.time order by smin.time) as seqnum
      from (select s.*,
                   min(cost) over (order by unix_timestamp(time) range between current row and 7200 following) as min_cost,
            from stock s
           ) s join
           stock smin
           on smin.cost = min_cost and
              unix_timestamp(smin.time) >= unix_timestamp(s.time) and
              unix_timestamp(smin.time) < unix_timestamp(s.time) + 7200
       ) s
where seqnum = 1;

暂无
暂无

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

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