我希望我能得到一些帮助,因为我在SQL编程方面还很陌生。
以下代码有效,但大约需要花费时间。运行21分钟。能否以相同的结果更快地执行该查询?
表EOD大约有4600万条记录和6列(数据类型跟随名称):
Symbol varchar(15)
Date Date
Open Float
High Float
Low Float
Close Float
Volume varchar(15)
该符号将根据我的分析而有所不同,因此未在CTE“ SMA”中编写where子句。
谢谢,让我知道是否可以说得更清楚。明智的。
with SMA as
(
select
*,
-- column names: symbol, open, low, high, close, volume(varchar(15))
((lag ([close],10)over (order by symbol, date) --#varSMANUM must also change number for field 'WM'!
+lag ([close],9)over (order by symbol, date)
+lag ([close],8)over (order by symbol, date)
+lag ([close],7)over (order by symbol, date)
+lag ([close],6)over (order by symbol, date)
+lag ([close],5)over (order by symbol, date)
+lag ([close],4)over (order by symbol, date)
+lag ([close],3)over (order by symbol, date)
+lag ([close],2)over (order by symbol, date)
+lag ([close],1)over (order by symbol, date)
)/ 10) as test,
(cast(2 as float)/(10 --#varSMANUM must add lag statements above!!!!
+1)) as WM
from
qwid.dbo.eod
)
select
date,
[close],
test
from
SMA
where
symbol = 'aapl'
order by
date;
答案 0 :(得分:2)
我想我先将查询重写为:
select date, [close],
avg([close]) over (order by symbol, date rows between 10 preceding and 1 preceding) as test
test
from qwid.dbo.eod
where symbol = 'aapl'
order by date;
然后,我要在qwid.dbo.eod(symbol, date, [close])
上添加索引。
请注意,这与您对前几个值的查询稍有不同。当您的版本返回NULL
时,这将返回一个计算值。