我使用以下查询来获取日期之间存在的所有值(作为参数传递):
SELECT date, share_id, close, sma50, sma100, sma200 FROM this, that, other
WHERE this.id = that.share_id
AND that.id = other.id
AND date <= '2014-08-13'
AND date >= '2014-07-31'
AND share_id IN (2625, 2626, 2628)
ORDER BY share_id, date;
返回数据:
date share_id close sma50 sma100 sma200
2014-07-31 2625 61963.00 62114.7200 60182.0200 55867.6050
2014-08-01 2625 61292.00 62150.6600 60248.8700 55931.0700
2014-08-04 2625 62108.00 62190.4600 60315.9900 55999.2950
2014-08-05 2625 61191.00 62218.3200 60370.9000 56064.2500
2014-08-06 2625 60233.00 62241.6000 60408.7700 56121.0900
2014-08-07 2625 59765.00 62243.2400 60432.4000 56173.6650
2014-08-08 2625 60522.00 62260.6800 60461.6200 56230.3400
2014-08-11 2625 61492.00 62300.0000 60511.3300 56286.7950
2014-08-12 2625 61450.00 62322.9000 60560.8300 56343.2450
2014-08-13 2625 61172.00 62318.4200 60594.6100 56398.4750
2014-07-31 2626 24606.00 24751.3800 24431.3722 24137.0023
2014-08-01 2626 24258.00 24752.0400 24427.6622 24142.5973
2014-08-04 2626 24353.00 24753.8400 24426.0459 24148.3023
2014-08-05 2626 24100.00 24757.0800 24425.9971 24155.1223
在技术分析中,移动平均线的交叉是可能趋势变化的信号。 是否可以隔离:
sma50
在此日期范围内从大于sma200
开始到小于sma200
?是否可以在单个查询中执行...找到sma50
在该时间段内针对每个特定共享sma200
越过的所有共享?
如果不在存储过程中迭代/循环日期,我就无法想出一种优雅的方法。
预期结果集(如果我们检查sma200下方的近似值,则更改上述最后记录的收盘价值):
2014-08-05 2626 24100.00 24757.0800 24425.9971 24155.1223
只是为了让一个更简单的例子,让它更清晰:
date sma200 sma50
2014-08-05 4080.4500 4022.2200
2014-08-06 4079.8700 4030.4600
2014-08-07 4079.4800 4039.7400
2014-08-08 4078.9050 4051.8400
2014-08-11 4079.3600 4066.3000
--->2014-08-12 4079.9750 4081.6600
2014-08-13 4079.7500 4093.4200
2014-08-14 4079.7000 4106.0000
2014-08-15 4079.4950 4117.7200
2014-08-18 4078.3300 4130.2400
2014-08-19 4077.1250 4141.8400
2014-08-20 4076.6350 4152.6600
2014-08-21 4075.6400 4162.8000
2014-08-22 4073.5950 4174.6400
使用的查询:
SELECT date, sma200, sma50
FROM this, dly, ndctrs
WHERE this.id = that.share_id
AND that.id = other.id
AND date >= '2014-06-05'
AND date <= '2014-08-22'
AND share_id = 4500;
预期结果:
date sma200 sma50 type
2014-08-12 4079.9750 4081.6600 over
可能的解决方案:
在此期间找到所有sma50&gt; = sma200的地方 找到所有sma50&lt;那个时期的sma200
如果两个数据集中都返回了超过1个结果,那么我们可以说发生了交叉,发现何时何地稍微困难
答案 0 :(得分:0)
Select date, share_id, close, sma50, sma100, sma200 From (
SELECT date, share_id, close, sma50, sma100, sma200 FROM this, that, other
WHERE this.id = that.share_id
AND that.id = other.id
AND date <= '2014-08-13'
AND date >= '2014-07-31'
AND share_id IN (2625, 2626, 2628)
ORDER BY share_id, date) as expr1
Where sma50 < sma200 group by share_id
我将您的查询集复制到表中以对其进行测试 - 您可能必须使用嵌套的方式调整一些小的语法错误,但原理有效。这将选择sma50大于sma200的所有记录,但Group By将其折叠为仅获取每个share_id的第一条记录。由于您已按日期对结果集进行了排序,因此它将为每个share_id选择最早的记录。