给定计算每个分组数据中的开/高/低/接近值的时间/间隔

时间:2014-12-10 10:46:25

标签: sql postgresql aggregate-functions window-functions

假设原始数据是:

  Timestamp   High Low Volume
  10:24.22345 100   99  10
  10:24.23345 110   97  20
  10:24.33455 97    89  40
  10:25.33455 60    40  50
  10:25.93455 40    20  60

采样时间为1秒,输出数据应如下(按秒排序):

  Timestamp   Open Close High  Low Volume 
  10:24         82   83   110   89  70     
  10:25         50   40   60    20  110    
  • Open表示群组中最早数据的价格
  • Close表示组中最新数据的价格
  • Volume表示组中的总和(卷)

抽样单位来自1 second, 5 sec, 1 minute, 1 hour, 1 day, ...

现在我可以通过以下SQL获得High,Low,Volume:

SELECT date_trunc(\'#{interval}\', ticktime) AS ticktime_stamp,
       max(bid_price) as high,
       min(bid_price) as low,
       sum(bid_volume) as volume,
       max(product_type) as product_type
FROM   czces
WHERE  ticktime >=  \'#{begin_time}\'::timestamp
AND  ticktime <  \'#{end_time}\'::timestamp
AND  product_type =\'#{product_type}\'
GROUP  BY 1
ORDER  BY ticktime_stamp ASC

但是如何根据上述查询获取每个组中的openclose值?

2 个答案:

答案 0 :(得分:2)

您可以将window functionsDISTINCT ON结合使用:

SELECT DISTINCT ON (1)
       date_trunc('#{interval}', ticktime) AS ticktime_stamp
     , max(bid_price)         OVER w AS high
     , min(bid_price)         OVER w AS low
     , sum(bid_volume)        OVER w AS volume
     , max(product_type)      OVER w AS product_type
     , min(product_type)      OVER w AS product_type
     , first_value(bid_price) OVER w AS open
     , last_value(bid_price)  OVER w AS close
FROM   czces
WHERE  ticktime >= '#{begin_time}'::timestamp
AND    ticktime <  '#{end_time}'::timestamp
AND    product_type ='#{product_type}'
WINDOW w AS (PARTITION BY date_trunc('#{interval}', ticktime) ORDER BY ticktime
             ROWS BETWEEN UNBOUNDED PRECEDING
                      AND UNBOUNDED FOLLOWING)
ORDER  BY 1;

自定义窗口框架的说明:

DISTINCT ON的解释:

答案 1 :(得分:1)

您正在寻找的查询很可能是这样,但如果没有实际的测试数据,则很难确定。

SELECT
  min(date_trunc('second', "Timestamp")) OVER minutes "Timestamp",
  first_value("Price") OVER minutes "Open",
  last_value("Price") OVER minutes "Close",
  max("High") OVER minutes "High",
  min("Low") OVER minutes "Low",
  sum("Volume") OVER minutes "Volume"
FROM czces
WINDOW minutes AS (
    PARTITION BY date_trunc('second', "Timestamp")
    ORDER BY "Timestamp"
);