You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Window Function同时获取最大价格及其对应Datetime?

解决方案

你可以用ClickHouse的argMax函数来实现需求,它能返回对应最大值的关联字段值。修改后的SQL如下:

with product_table as (
    select * from data
    where product_name='x' 
    and datetime between parseDateTimeBestEffort('2023-01-25 10:00:00.0')
                and parseDateTimeBestEffort('2023-01-25 11:00:00.0')
    order by datetime)
, max_price_table as (
    select 
        datetime
        , max(price) over wndw as max_price
        , argMax(datetime, price) over wndw as max_price_datetime
    from product_table
    window wndw as (order by toFloat64(datetime) range
        between 1 following and 60 following)
)
select * from max_price_table

说明

  • argMax(datetime, price) 函数会在当前窗口范围内,找到price最大值对应的datetime,这样就能同时拿到最高价和它出现的时间。
  • 原SQL里的datetime是当前行的时间,新增的max_price_datetime才是窗口内最高价出现的具体时间。

内容的提问来源于stack exchange,提问作者MoneyBall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 16:04:57