三角矩阵乘法代

问题描述:

试图产生一个三角矩阵乘法在T-SQL- 就像一个三角矩阵的乘法看起来就像这样:三角矩阵乘法代

 
0 
0 1 
0 2 4 
0 3 6 9 
0 4 8 12 16 

我一直没能找到这个有效的解决方案。任何帮助表示赞赏。

+2

请停止发送。 – 2010-08-17 19:06:45

+0

是否需要专门在SQL中完成,还是有其他选项可用? – AllenG 2010-08-17 19:07:06

+1

它的一个COBOL问题! – 2010-08-17 19:07:31

有一个聪明的方式与XML(SQL 2005及更高版本)要做到这一点:(永久订购数量表是一个好主意)

with Nums(n) as (
    select 0 union all 
    select 1 union all 
    select 2 union all 
    select 3 union all 
    select 4 union all 
    select 5 union all 
    select 6 union all 
    select 7 -- resize as needed or make it permanent 
) 
    select x as Prod from Nums 
    cross apply (
    select Cast(Nums.n*(N2.n) as varchar(80))+space(3-Len(Nums.n*N2.n)) 
    -- expand the varchar size if needed 
    as [text()] 
    from Nums as N2 
    where Nums.n >= n 
    order by N2.n 
    for xml path('') 
) as X(x) 
    where n <= 4 -- Adjust as needed 
    order by n; 

输出是这样的:

Prod 
-------- 
0 
0 1 
0 2 4 
0 3 6 9 
0 4 8 12 16