转换要在MS Access中使用的SQL Server查询

时间:2017-10-08 13:01:52

标签: sql sql-server ms-access

我在T-SQL中有一个工作查询,但需要在MS Access中转换它(并使用它)。我尝试运行时遇到错误:

SELECT 
    tblKPIData.id, tblKPIData.KPI_id,
    tblKPI.KPI_Name,
    tblKPIData.ImportTimestamp, tblKPIData.Quantity,
    tblKPIData.FinancialMonth, tblKPIData.FinancialYear,
    tblKPIData.Zone_id, tblZone.ZoneName,
    tblKPIData.DMA_id, tblDMA.DMA_Name,
    tblKPIData.TargetOrResult
FROM 
    ((((tblKPIData
INNER JOIN 
    tblKPI ON tblKPI.Id = tblKPIData.KPI_id)
LEFT JOIN 
    tblDMA ON tblDMA.Id = tblKPIData.DMA_id)
LEFT JOIN 
    tblZone ON tblZone.ID = tblKPIData.Zone_id)
INNER JOIN 
    tblDashboardKPI ON tblDashboardKPI.KPI_Id = tblKPIData.KPI_id)
INNER JOIN
    (SELECT 
         a.kpi_id, a.financialMonth, a.financialYear,
         ISNULL(a.zone_id, 0) AS zone_id,
         ISNULL(a.dma_id, 0) AS dma_id,
         a.targetorresult,
         MAX(a.importtimestamp) AS importtimestamp
     FROM
         tblKPIData a
     GROUP BY 
         kpi_id, financialMonth, financialYear,
         zone_id, dma_id, targetorresult) AS max_kpi ON (tblKPIData.KPI_id = max_kpi.KPI_id
                                                     AND tblKPIData.ImportTimestamp = max_kpi.importtimestamp
                                                     AND tblKPIData.FinancialMonth = max_kpi.FinancialMonth
                                                     AND tblKPIData.FinancialYear = max_kpi.FinancialYear
                                                     AND ISNULL(tblKPIData.Zone_id, 0) = ISNULL(max_kpi.zone_id, 0)
                                                     AND ISNULL(tblKPIData.DMA_id, 0) = ISNULL(max_kpi.dma_id, 0)
                                                     AND tblKPIData.TargetOrResult = max_kpi.TargetOrResult)
WHERE 
    tblKPIData.FinancialMonth = 'Oct'
    AND tblKPIData.FinancialYear = 2017
    AND tblKPIData.KPI_id IN (SELECT kpi_id FROM tblDashboardKPI WHERE tblDashboardKPI.KPI_Id = tblKPIData.KPI_id)
    AND (tblKPIData.Zone_id = 5 OR tblKPIData.DMA_id IN (SELECT id FROM tblDMA WHERE Zoneid = 5))
    AND ((tblDashboardKPI.Status) = True)
    AND ((tblDashboardKPI.Dashboard_Id) = 6)

我收到此错误:

  

查询表达式中函数使用的参数数量错误&t; tblKPIData.KPI_Id = max_kpi.KPI_id和tblKPIData.ImportTimestamp = max_kpi.importtimestamp和tblKPIData.FinancialMonth = max_kpi.FinancialMonth and ... etc

谁能告诉我有什么问题?我真的不知道......

1 个答案:

答案 0 :(得分:3)

ISNULL(Field)是有效的Access SQL,但返回与Field Is Null相同的内容(如果字段为True则为布尔Null,如果为{n}则为False #39; T)。

ISNULL(Field, 0)无效访问SQL,因为ISNULL只接受一个参数(因此错误的参数数量错误)。

如同Gordon Linoff所说,如果您想将Nz替换为Null,则需要使用0

只需使用ISNULL

查找并替换所有NZ即可