I got a definition of maximum drawdown from an investment website (thanks Google!). So I think we need to calculate the percentage drop between the highest point in a graph and its subsequent lowest point.
The following query calculates the maximum drawdown on investments in Oracle stock over the last twelve months. It joins the investments
table to itself, with aliases to distinguish the versions of the table (one for the highest peak, one for the lowest trough). This may not mirror your precise business logic, but it shows the SQL techniques which Oracle offers you.
select round(((max_return-min_return)/max_return)*100, 2) as max_drawdown
from
( select max(t1.return_amt) as max_return
, min(t2.return_amt) as min_return
from investments t1
join investments t2
on ( t1.stock_id = 'ORCL'
and t2.stock_id = t1.stock_id
and t2.created_date > t1.created_date )
where t1.created_date >= add_months(sysdate, -12)
and t2.created_date >= add_months(sysdate, -12)
)
/
This query will return zero if the stock has not experienced a drop during the window. It also does not check for a following upturn (as I understand drawdown it is supposed to be the bottom of a trough, a point we can only establish once the stock has started to climb again).
With regard to training at home, we can download software from Oracle TechNet for that purpose. If bandwidth or disk space are an issue go for the Express Edition; it doesn't have all the features but you probably won't want them for a while yet. Oracle do provide a free IDE, SQL Developer. As its name suggests it is primarily targeted at developers but it has many of the DBA-oriented features of DB Artisan. For full-on database management Oracle offers Enterprise Manager.
edit
In the comments outis suggests
You could add a t1.return_amt >
t2.return_amt
in the join as a minor
optimization
I think it is unlikely that return_amt
would be indexed, so I think it is unlikely that such a clause would have an impact on performance. What it would do is change the behaviour for stocks which do not have a drawdown. The query I presented returns zero for stocks which have increased continuously through the time window. The additional filter would return a NULL in such a case. Which is the more desirable outcome is a matter of taste (or requirements spec).