2014-01-16 35 views
-1

我下表獲取最後價值

+-----------------------+-------+----------+---------+----------+ 
|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 

問題是,它得到從該分鐘或間隔的所有值,我只需要不是所有的最後一個值

在此先感謝

+0

添加(MAX [日期])與MAX – Mihai

+0

does not工作已經檢查 –

回答

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]值屬於哪個週期的一個。根據你的敘述,我最初有/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