Admitedly pas testé à fond - mais comment sur l'utilisation d'un CTE et ROWNUMBER() pour faire en deux étapes
1) Identifier tous les hi hi suivants pour chaque ligne 2) toute rangée dont la rangée suivante immédiate a une hauteur inférieure à la ligne courante - alors la ligne courante doit être un maximum local.
ou quelque chose comme ça:
begin
DECLARE @highTable as table (high bigint, day date)
declare @securityid int,
@start datetime,
@end datetime
set @start = '1-1-2010'
set @end = '2-1-2010'
select @securityid = id from security where riccode = 'MSFT.OQ' ;
with highsandlows_cte as (
SELECT
ROW_NUMBER() over (order by day) i
, high
, day
, (select top 1 day from quotes nextHi where nextHi.high > today.high and nextHi.day >= today.day and nextHi.securityId = today.securityId order by day asc) nextHighestDay
FROM
quotes today
WHERE
today.securityid = @securityid)
select
*
, (Coalesce((select 1 from highsandlows_cte t2 where t1.i + 1 = t2.i and t1.nextHighestDay > t2.nextHighestDay),0)) as isHigh
from
highsandlows_cte t1
order by
day
end
ok ce qui précède est faux - cela semble être plus sur la bonne voie:
begin
DECLARE @highTable as table (high bigint, day date)
declare @securityid int,
@start datetime,
@end datetime
set @start = '1-1-2010'
set @end = '2-1-2010'
select @securityid = id from security where riccode = 'MSFT.OQ' ;
with highsandlows_cte as (
SELECT
ROW_NUMBER() over (order by day) i
, high
, day
, low
FROM
quote today
WHERE
today.securityid = @securityid and today.day > convert(varchar(10), @start, 111) and convert(varchar(10), @end, 111) >today.day)
select
cur.day
, cur.high
, cur.low
, case when ((cur.high > prv.high or prv.high IS null)and(cur.high > nxt.high or nxt.high is null)) then 1 else 0 end as isLocalMax
, case when ((cur.low < prv.low or prv.low IS null)and(cur.low < nxt.low or nxt.low is null)) then 1 else 0 end as isLocalMin
from
highsandlows_cte cur left outer join highsandlows_cte nxt
on cur.i + 1 = nxt.i
left outer join highsandlows_cte prv
on cur.i - 1 = prv.i
order by
cur.day
end
Obtenez des problèmes avec les doublons (Highs/bas), bien que ...
Copie possible de: [calcul de "Max Draw Down" dans SQL] (http://stackoverflow.com/questions/4045177/calculating-max-draw-down- en-sql) –
plus d'un compliment qu'un duplicata je pense. – Ramy
plus d'un complément que d'un compliment, je pense. ;) –