在SQL Server中获得产品有两种选择。
1.使用日志和指数进行模拟:
sqlfiddle
create table returns( returnDate date, returnValue float)insert into returns values('05/31/06', -0.002271835)insert into returns values('06/30/06', -0.001095986)insert into returns values('07/31/06', 0.006984908)insert into returns values('08/31/06', 0.014865360)insert into returns values('09/30/06', 0.008938896)select totalReturn = power ( cast(10.0 as float) , sum(log10(returnValue + 1.0)) ) - 1from returns;with tr as( select totalReturn = power ( cast(10.0 as float) , sum(log10(returnValue + 1.0)) ) - 1 , months = cast(count(1) as float) from returns)select annualized = power(totalReturn + 1, (1.0 / (months / 12.0))) - 1from tr;这利用日志和指数来模拟产品计算。更多信息:用户定义的函数。
这里的一个问题是返回<-100%失败。如果您不希望这些设置没问题,那么您需要将所有<100%到-100%的值设置。
然后,您可以根据需要使用此实际收益来获得年度收益。
2.使用CLR定义自定义聚合:
参见在线书籍。
您可以创建一个CLR自定义函数,然后将其链接在一起以在查询中使用。这是更多的工作,您必须在服务器上启用CLR,但是一旦完成,就可以根据需要使用它。



