从间隔获得最高和最低

时间:2014-08-06 17:04:45

标签: sql

我有以下条目,我想提取数据以创建OHLC蜡烛15分钟(问题是SQL标签,但我无法创建新标签)。 所以我想每15分钟我需要最高的high_price最低的lo​​w_price第一个open_price和last_price

last_price|high_price|low_price|buy_price|sell_price|volume|volumeCurrent|open_price|STIME
11.3789 11.381  11.3789 11.33   11.4213 4767947 0   11.381  2014-05-25 20:15:00.000
11.3789 11.381  11.3789 11.33   11.4213 4767947 0   11.381  2014-05-25 20:14:00.000
11.3789 11.381  11.3789 11.33   11.4213 4768170 0   11.381  2014-05-25 20:13:00.000
11.3789 11.3789 11.3789 11.33   11.3789 4768000 0   11.3789 2014-05-25 20:12:00.000
11.3789 11.3789 11.3789 11.33   11.3789 4767787 0   11.3789 2014-05-25 20:11:00.000
11.392  11.31   11.392  11.33   11.4213 4767897 0   11.31   2014-05-25 20:10:00.000
11.392  11.392  11.37   11.33   11.392  4767643 0   11.37   2014-05-25 20:09:00.000
11.37   11.37   11.37   11.31   11.37   4767633 0   11.37   2014-05-25 20:08:00.000
11.37   11.3112 11.37   11.31   11.4213 4767633 0   11.3112 2014-05-25 20:07:00.000
11.37   11.37   11.37   11.31   11.37   4767642 0   11.37   2014-05-25 20:06:00.000
11.37   11.37   11.37   11.31   11.37   4767631 0   11.37   2014-05-25 20:05:00.000
11.33   11.37   11.33   11.33   11.37   4767620 0   11.37   2014-05-25 20:04:00.000
11.33   11.3149 11.33   11.31   11.4213 4767617 0   11.3149 2014-05-25 20:03:00.000
11.33   11.3149 11.33   11.31   11.4213 4767655 0   11.3149 2014-05-25 20:02:00.000
11.33   11.33   11.33   11.33   11.4213 4767386 0   11.33   2014-05-25 20:01:00.000
11.331  11.332  11.331  11.331  11.4213 4763363 0   11.332  2014-05-25 20:00:00.000

我试过这样的事情。问题是,我需要最后的high_price和last low_Price,而我需要15分钟内最高 high_price 和最低 low_price 。可能我需要使用 max(highprice) min(lowprice),但我不知道如何。

SELECT 
        last_price,
        high_price,
        low_price,
        buy_price,
        sell_price,
        volume,
        volumeCurrent,
        open_price,
        date
FROM 
    m_cPrice 
WHERE 
    DATEPART(MINUTE,date)%15=0 and curency = '6'
ORDER BY 
    date desc

2 个答案:

答案 0 :(得分:0)

SELECT
    MAX(high_price),
    MIN(low_price)
FROM m_cPrice
WHERE ...

由于ORDER BYMAX()的隐式分组,可以跳过此查询MIN()

答案 1 :(得分:0)

我想你想要这样的东西:

SELECT min(date) as dt, max(high_price), min(low_price)
FROM m_cPrice 
WHERE currency = '6'
GROUP BY (DATEPART(MINUTE,date) / 15)
ORDER BY min(date) desc;

group by占用分钟部分并除以15. SQL Server执行整数除法,因此这应该是0到3之间的数字。