首先,如OMG Ponies所述,您不能通过它们的序号位置来引用列。这不是意外。 SQL规范不是为DDL或DML中的动态模式构建的。
鉴于这一点,我不得不想知道为什么你的数据结构与你一样。当您尝试提取信息时,架构和问题域之间不匹配的迹象会自动增加。当查询编写起来非常麻烦时,这表明模式没有正确地为其设计的域建模。
然而,是因为它可以给你告诉我们,一个替代的解决方案是类似以下内容:(我假设field_1*field1
本来是field_1 * field_1
或field_1
方形或Power(field_1, 2)
)
Select 1 As Sequence, field_1 As [Field], Sfield_1 As [SField], Sfiled_1 As [SFiled]
Union All Select 2, field_2, Sfield_2, Sfiled_2
...
Union All Select n, field_n, Sfield_n, Sfiled_n
现在你的查询看起来像:
With Inputs As
(
Select 1 As Sequence, field_1 As [Field], Sfield_1 As [SField], Sfiled_1 As [SFiled]
Union All Select 2, field_2, Sfield_2, Sfiled_2
....
)
, Results As
(
Select Case
When Sequence = 1 Then Power([Field], 2) - ([SField] * [SFiled])
Else 1/Power([Field], 2) - ([SField] * [SFiled])
End
As Result
From Inputs
)
Select Exp(Sum(Log(Result)))
From Results
的[是否有可能使用柱顺序位置选择SQL Server数据]可能的复制(http://stackoverflow.com/questions/368505/is-it-possible-to-select-sql-server-数据使用列顺序位置) – 2015-10-07 05:40:26