我是该站点的新手,我对我在 SQL Server 2008 中重做的 Access 表单中的一些代码有疑问,它在一行中包含 SUM、IIF 和 IsNumeric,我对如何重写它感到困惑在 SQL 中。
Sum([Employee COUNT]*IIf(IsNull([2011]),0,CDbl([2011]))*IIf(IsNumeric([Length]),CDbl([Length]),0)) AS Cost
我是该站点的新手,我对我在 SQL Server 2008 中重做的 Access 表单中的一些代码有疑问,它在一行中包含 SUM、IIF 和 IsNumeric,我对如何重写它感到困惑在 SQL 中。
Sum([Employee COUNT]*IIf(IsNull([2011]),0,CDbl([2011]))*IIf(IsNumeric([Length]),CDbl([Length]),0)) AS Cost
我相信这会满足您的需求:
Sum([Employee COUNT]
* (case
when [2011] Is Null
then 0
else cast([2011] as float)
end)
* (case
when IsNumeric([Length])= 1
then cast([Length] as float)
else 0
end)) AS Cost
SUM([Employee Count]
* COALESCE(
[2011],
CASE WHEN
ISNUMERIC([2011]) = 1
THEN
CAST([2011] AS DECIMAL(18,4)
ELSE
0
END
)
* CASE WHEN
ISNUMERIC([Length]) = 1
THEN
-- choose your own precision here.
CAST ([Length] AS DECIMAL(18,4))
ELSE
0
END
) AS Cost