T-SQL到MS Access SQL

时间:2017-10-06 10:46:32

标签: sql-server tsql ms-access

我在SQL Server中有一个工作查询,但我无法将其转换为MS Access SQL。

这是SQL Server中的工作查询:

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 = KPI_id
left outer join tblDMA
on tblDMA.Id = tblKPIData.DMA_id
left outer 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) = 1)
and ((tblDashboardKPI.Dashboard_Id) = 6)

我知道我必须添加括号,但不确定在哪里。这就是我提出的,但是我得到一个错误(不支持Join Expression),它在第一个连接上突出显示了一段代码(tblKPI.Id = KPI_id):

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 = KPI_id)
left outer join tblDMA
on tblDMA.Id = tblKPIData.DMA_id)
left outer 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) = 1)
and ((tblDashboardKPI.Dashboard_Id) = 6)

我做错了什么?

0 个答案:

没有答案