Home » SQL & PL/SQL » SQL & PL/SQL » months bettween
months bettween [message #22114] Tue, 24 September 2002 06:14 Go to next message
karen
Messages: 19
Registered: January 2002
Junior Member
I'm calculating standard deviation for funds based upon a performance table which has monthly cumulative returns and keyed by fund_id, and as_of_d. However, before I run the standard deviation calculation I need to first check that based on whatever month I'm in I have 36 months of previous returns (36 entries). I tried this
select fund_id, as_of_d, min(as_of_d)
from
t_iwf3_fund_3yr
where
fund_id = 6022
group by fund_id
having months_between(as_of_d, min(as_of_d)) > 2

but I'm getting a "group function not allowed here" returned. When I add as_of_d to the group by statement it gets the minimum date for that row. Any help would be greatly appreciated. I'm tearing my hair out here!
Re: months between [message #22121 is a reply to message #22114] Tue, 24 September 2002 09:29 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Karen, maybe this will give you some ideas. This query will list every row that has at least 36 months of previous history (assuming an entry is made every month). You can add to the WHERE clause to filter to the current month, etc.:

select *
  from t t1
 where exists (select null
                 from t t2
                where t2.fund_id = t1.fund_id
                  and t2.as_of_date <= months_between(t1.as_of_date, -36));
Re: months between [message #22128 is a reply to message #22114] Tue, 24 September 2002 14:40 Go to previous messageGo to next message
karen
Messages: 19
Registered: January 2002
Junior Member
Thanks Todd. I tried this and it returned an error

ORA-00932; inconsistent datatypes
Re: months between [message #22130 is a reply to message #22114] Tue, 24 September 2002 16:25 Go to previous message
karen
Messages: 19
Registered: January 2002
Junior Member
Thank you so much Todd. I really appreciate your help in this. It means that I get to go home at a decent hour tonight. Thanks again!
Previous Topic: Re: Retrieving records N to N+10
Next Topic: RECORD parameter (follow-up question)
Goto Forum:
  


Current Time: Wed May 08 16:06:52 CDT 2024