从一分钟获取最后一个值

时间:2014-01-16 14:08:25

标签: sql tsql

我有下表

+-----------------------+-------+----------+---------+----------+
|date                   |curency|high_price|low_price|last_price|
+-----------------------+-------+----------+---------+----------+
|2014-01-16 16:01:42.000|2      |24.98     |23.9     |24.2      |
+-----------------------+-------+----------+---------+----------+
|2014-01-16 16:01:32.000|2      |24.98     |23.9     |24.12202  |
+-----------------------+-------+----------+---------+----------+
|2014-01-16 16:01:22.000|2      |24.98     |23.9     |24.12202  |
+-----------------------+-------+----------+---------+----------+
|2014-01-16 16:01:12.000|2      |24.98     |23.9     |24.21626  |
+-----------------------+-------+----------+---------+----------+
|2014-01-16 16:01:02.000|2      |24.98     |23.9     |24.11102  |
+-----------------------+-------+----------+---------+----------+
|2014-01-16 16:00:52.000|2      |24.98     |23.9     |24.21628  |
+-----------------------+-------+----------+---------+----------+
|2014-01-16 16:00:42.000|2      |24.98     |23.9     |24.2      |
+-----------------------+-------+----------+---------+----------+
|2014-01-16 16:00:32.000|2      |24.98     |23.9     |24.2      |
+-----------------------+-------+----------+---------+----------+

我使用以下查询按时间间隔分组,例如15分钟:

WITH x AS (
    SELECT 
        last_price,
        high_price,
        low_price, 
        dateadd(MINUTE, datediff(MINUTE, 0,[date])/1*1,0) AS SERVERTIME
    FROM 
        m_cPrice 
    WHERE 
        curency=2 
    GROUP BY 
        last_price,
        high_price,
        low_price,
        datediff(MINUTE, 0,[date])/1*1 
)
SELECT 
    last_price,
    high_price,
    low_price , 
    dateadd(MINUTE, datediff(MINUTE, 0,SERVERTIME)/1*1,0) as STIME 
FROM 
    x 
WHERE 
    DATEPART(MINUTE,SERVERTIME)%15=0
ORDER BY 
    STIME DESC

问题是它从该分钟或间隔获得所有值,而我只需要最后一个值而不是所有值

提前致谢

1 个答案:

答案 0 :(得分:1)

这似乎有效,但鉴于样本数据的范围有限,有点棘手:

declare @t table([date] datetime,curency /* sic */ int, high_price decimal(38,5),
                 low_price decimal(38,5), last_price decimal(38,5))
insert into @t([date],curency, high_price,  low_price,   last_price) values
('2014-01-16T16:01:42.000',2,24.98,23.9,24.2    ),
('2014-01-16T16:01:32.000',2,24.98,23.9,24.12202),
('2014-01-16T16:01:22.000',2,24.98,23.9,24.12202),
('2014-01-16T16:01:12.000',2,24.98,23.9,24.21626),
('2014-01-16T16:01:02.000',2,24.98,23.9,24.11102),
('2014-01-16T16:00:52.000',2,24.98,23.9,24.21628),
('2014-01-16T16:00:42.000',2,24.98,23.9,24.2    ),
('2014-01-16T16:00:32.000',2,24.98,23.9,24.2    )

;With Periods as (
    select *,
        DATEADD(minute,((DATEDIFF(minute,0,[date])/1)*1),0) as DatePeriod
    from @t
), Ordered as (
    select *,
        ROW_NUMBER() OVER (PARTITION BY DatePeriod
                            ORDER BY [date] desc) as rn
    from Periods
)
select DatePeriod,last_price
from Ordered where rn = 1

Periods CTE是确定每个[date]值属于哪个时段的CTE。根据你的叙述,我最初有/15)*15)得到15分钟的时间间隔,然后发现你只给了我们两分钟的数据,所以它现在是/1)*1)(现在可以完全删除)。

这给了我们:

DatePeriod              last_price
----------------------- ---------------------------------------
2014-01-16 16:00:00.000 24.21628
2014-01-16 16:01:00.000 24.20000