聽起來像是你正在尋找一個常量組表示的時間跨度的記錄。您可以通過定義控制日期集來實現。
下面是一個簡單的查詢(不考慮週末和假期,但可以添加):
填充:
CREATE TABLE #quotes
(
id int identity(1,1)
,security_id VARCHAR(50)
,last_price FLOAT
,bid_price FLOAT
,ask_price FLOAT
,[date] DATETIME
,[timestamp] DATETIME
,trade_volume FLOAT
,cumulative_volume FLOAT
,average_volume FLOAT
,created_at DATETIME
)
DECLARE @i int
set @i = 100000
WHILE @i > 0
BEGIN
INSERT INTO #quotes (
security_id
,last_price
,bid_price
,ask_price
,[date]
,[timestamp]
,trade_volume
,cumulative_volume
,average_volume
,created_at
)
values('IBM US'
, 100.00 + RAND()
, 100.00 + RAND()
, 100.00 + RAND()
, DATEADD(MINUTE, -1* @i, GETDATE())
, DATEADD(MINUTE, -1* @i, GETDATE())
, 10000000.00 + RAND()*1000000.00
, 10000000.00 + RAND()*1000000.00
, 10000000.00 + RAND()*1000000.00
,getdate())
set @i= @i-1
END
就可以在時間跨度改變,但下面就給你大約有1000條記錄代表從頭到尾的集合。
DECLARE @StartDate DATETIME,
@EndDate DATETIME,
@j FLOAT,
@step FLOAT
set @StartDate = GETDATE()-20
SET @EndDAte = GETDATE()
set @j = 0.0
CREATE TABLE #TimeTable
(
IntervalDate DATETIME
)
--say you always want 1000 measures
--use the datediff value to define the step size:
select @step = DATEDIFF(MINUTE, @StartDate, @EndDate)/1000.0
WHILE @j < DATEDIFF(MINUTE, @StartDate, @EndDate)
BEGIN
INSERT #TimeTable (IntervalDate) VALUES (DATEADD(minute, @j, @StartDate))
SET @j = @[email protected]
print @j
END
select security_id
,last_price
,bid_price
,ask_price
,[date]
,[timestamp]
,trade_volume
,cumulative_volume
,average_volume
,created_at
from #Quotes q
join #TimeTable t on dateadd(mi, datediff(mi, 0, q.date), 0) = dateadd(mi, datediff(mi, 0, t.IntervalDate), 0)
表結構和數據的一個例子會很好。否則,你可能只會得到vage的建議。 –
用結構更新了問題。 –