|
|
Archives of the TeradataForum
Message Posted: Thu, 15 Sep 2005 @ 16:49:56 GMT
Subj: | | Re: Factorial in Teradata |
|
From: | | Spaleny Ivo |
You can use logarithms.
Ivo Spaleny
SQL code:
/* prepare lookup */
create volatile table tmp_NumberList(X integer not null) unique primary
index (X);
BT;
/*fill lookup*/
insert tmp_NumberList(X) values (0);
insert tmp_NumberList(X) select MX+X+1 from tmp_NumberList A cross join
(select max(X) MX from tmp_NumberList ) B ;
insert tmp_NumberList(X) select MX+X+1 from tmp_NumberList A cross join
(select max(X) MX from tmp_NumberList ) B ;
insert tmp_NumberList(X) select MX+X+1 from tmp_NumberList A cross join
(select max(X) MX from tmp_NumberList ) B ;
insert tmp_NumberList(X) select MX+X+1 from tmp_NumberList A cross join
(select max(X) MX from tmp_NumberList ) B ;
insert tmp_NumberList(X) select MX+X+1 from tmp_NumberList A cross join
(select max(X) MX from tmp_NumberList ) B ;
insert tmp_NumberList(X) select MX+X+1 from tmp_NumberList A cross join
(select max(X) MX from tmp_NumberList ) B ;
insert tmp_NumberList(X) select MX+X+1 from tmp_NumberList A cross join
(select max(X) MX from tmp_NumberList ) B ;
insert tmp_NumberList(X) select MX+X+1 from tmp_NumberList A cross join
(select max(X) MX from tmp_NumberList ) B
where MX+X+1<=170 /* 171 .. overflow of FLOAT/DOUBLE factorial */
;
/* select factorials */
select A.X,COALESCE(EXP(SUM(LN(B.X (FLOAT)))),1)
from tmp_NumberList A
left join tmp_NumberList B on B.X between 1 and A.X
where A.X<=170 /* 171! ... overflow of FLOAT/DOUBLE factorial */
group by A.X
order by 1;
ET;
/* clean up */
drop table tmp_NumberList;
| |