从T-SQL转换为Access查询?

时间:2015-05-29 19:52:40

标签: sql ms-access

有没有方便的方法将此T-SQL查询转换为MS Access? ROUNDCOALESCE似乎无法在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]

enter image description here

1 个答案:

答案 0 :(得分:0)

使用NZ代替COALESCENULLIFIIF代替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