我有一个使用{1}}语句的访问代码,这些语句有2合1,我试图将它们作为IIF
语句重写为SQL,但我正在点击一些speedbump。
CASE
第一个Sum(IIf([RRDD] Not Like "12*"
And [RRDD] Not Like "13*"
And [RRDD] Not Like "16*"
And [RRDD] Not Like "17*"
And [RRDD] Not Like "2706"
And [RRDD] Not Like "2707"
And [RRDD] Not Like "2331",
CDbl([2011 Total])*IIf(IsNumeric([Dur_In_Hours]),
CDbl([Dur_In_Hours]),0),0)) AS SP_DOM_COST
乘以第二个IIF是我困惑的地方,我需要它是SQL格式
答案 0 :(得分:2)
您可以简化SQL Server和Access中的逻辑。这是SQL Server版本:
Sum(case when left([RRDD], 2) not in ('12', '13', '16', '17') and
RRDD not in ('2706', '2707', '2331') and
isnumeric(dur_in_hours) = 1
then [2011 Total] * cast(Dur_In_Hours as float)
else 0.0
end) AS SP_DOM_COST
在两个系统中,like
将比通过常量列表搜索产生更多的开销。演员阵容是安全的,因为它在where
条款中(我也在上面的参考文献中添加答案,因为答案不正确)。
iif
的替换通常是case
,虽然此功能已添加到SQL Server 2012中(我说希望没有人使用它)。
答案 1 :(得分:1)
Sum(case when [RRDD] Not Like '12%'
And [RRDD] Not Like '13%'
And [RRDD] Not Like '16%'
And [RRDD] Not Like '17%'
And [RRDD] Not Like '2706'
And [RRDD] Not Like '2707'
And [RRDD] Not Like '2331'
then cast([2011 Total] as float) *
case when ISNUMERIC([Dur_In_Hours]) = 1
then cast([Dur_In_Hours] as float)
else 0
end
else 0
end) AS SP_DOM_COST
虽然IsNUmeric
是有效的TSQL函数,但它是apparently not very nice to play with。如果您得到奇怪的结果,请查看其他existing答案以获得解决方法。根据您的数据,您可能会很好。