有没有方便的方法将此T-SQL查询转换为MS Access? ROUND
和COALESCE
似乎无法在Access中使用。
我正在尝试让自定义AVG
字段在Access中运行,但始终会出现语法错误(缺少运算符)。
SELECT TOP 50 [EmployeeID]
,[Last Name]
,[First Name]
,[HrsThisWk]
--AVG HRs
,ROUND(
coalesce((
coalesce([1WksAgo],0) +
coalesce([2WksAgo],0) +
coalesce([3WksAgo],0) +
coalesce([4WksAgo],0) +
coalesce([5WksAgo],0)
)/
nullif(
case when coalesce([1WksAgo],0)=0 then 0 else 1 end +
case when coalesce([2WksAgo],0)=0 then 0 else 1 end +
case when coalesce([3WksAgo],0)=0 then 0 else 1 end +
case when coalesce([4WksAgo],0)=0 then 0 else 1 end +
case when coalesce([5WksAgo],0)=0 then 0 else 1 end,
0),0), 2)
AS '--- AVG HRs ---'
,[1WksAgo]
,[2WksAgo]
,[3WksAgo]
,[4WksAgo]
,[5WksAgo]
FROM [CMHR].[dbo].[tbl52weekHours_20150527]
GROUP BY
[EmployeeID]
,[Last Name]
,[First Name]
,[HrsThisWk]
,[1WksAgo]
,[2WksAgo]
,[3WksAgo]
,[4WksAgo]
,[5WksAgo]
答案 0 :(得分:0)
使用NZ
代替COALESCE
和NULLIF
,IIF
代替CASE WHEN
和"..."
代替[...]
这样:
SELECT TOP(50) "EmployeeID"
,"Last Name"
,"First Name"
,"HrsThisWk"
,ROUND(
NZ((
NZ("1WksAgo",0) +
NZ("2WksAgo",0) +
NZ("3WksAgo",0) +
NZ("4WksAgo",0) +
NZ("5WksAgo",0)
)/
NZ(
IIF(NZ("1WksAgo",0)=0, 0, 1) +
IIF(NZ("2WksAgo",0)=0, 0, 1) +
IIF(NZ("3WksAgo",0)=0, 0, 1) +
IIF(NZ("4WksAgo",0)=0, 0, 1) +
IIF(NZ("5WksAgo",0)=0, 0, 1),
0),0), 2)
AS "AVG HRs"
,"1WksAgo"
,"2WksAgo"
,"3WksAgo"
,"4WksAgo"
,"5WksAgo"
FROM "tbl52weekHours_20150527"
GROUP BY
"EmployeeID"
,"Last Name"
,"First Name"
,"HrsThisWk"
,"1WksAgo"
,"2WksAgo"
,"3WksAgo"
,"4WksAgo"
,"5WksAgo"
您可以在不TOP(50)
的情况下创建查询,然后将Top Values property设置为50
。