2017-09-06 138 views
0

我有一個SQL查詢,我想在MS Access中使用,但我無法正確理解。任何幫助將是明智的。轉換/翻譯t-sql查詢到MS Access查詢

select tblKPIData.Id 
    , tblKPIData.KPI_id 
    , tblKPIData.Quantity 
    , tblKPIData.FinancialMonth 
    , tblKPIData.FinancialYear 
    , tblKPIData.ImportTimestamp 
    , tblDashboadKPI.Dashboard_Id 
from (
    select tblKPIData.kpi_id, 
      max(tblKPIData.ImportTimestamp) as ImportTimestamp 
    from tblKPIData 
    group by tblKPIData.KPI_id 
) 
as b 
inner join tblKPIData 
on tblKPIData.KPI_id = b.KPI_id 
and tblKPIData.ImportTimestamp = b.ImportTimestamp 
right join tblDashboadKPI 
on tblDashboadKPI.KPI_Id = tblKPIData.KPI_id 
where FinancialMonth = 'nov' 
and FinancialYear = 2016 
and tblDashboadKPI.Dashboard_Id = 5 
order by tblKPIData.Id 
     ,tblKPIData.KPI_id 
+1

你卡在哪裏? – RealCheeseLord

回答

1

如果查詢當前工作,我建議:

select kp.Id, kp.KPI_id, kp.Quantity, kp.FinancialMonth, 
     kp.FinancialYear, kp.ImportTimestamp, 
     d.Dashboard_Id 
from ((select tblKPIData.kpi_id, 
       max(tblKPIData.ImportTimestamp) as ImportTimestamp 
     from tblKPIData 
     group by tblKPIData.KPI_id 
    ) as b inner join 
     tblKPIData as kp 
     on kp.KPI_id = b.KPI_id and kp.ImportTimestamp = b.ImportTimestamp 
    ) right join 
    tblDashboadKPI as d 
    on d.KPI_Id = kp.KPI_id 
where FinancialMonth = "nov" and FinancialYear = 2016 and 
     d.Dashboard_Id = 5 
order by kp.Id, kp.KPI_id; 

變化:

  • JOIN的需要有額外的括號
  • 字符串使用雙quots
  • 我也引入了表別名,以簡化查詢
1

我想我有解決方案:

SELECT tblKPIData.Id 
    , tblKPIData.KPI_id 
    , tblKPIData.Quantity 
    , tblKPIData.FinancialMonth 
    , tblKPIData.FinancialYear 
    , tblKPIData.ImportTimestamp 
    , tblKPIData.Type 
    , tblDashboadKPI.Dashboard_Id 
FROM tblDashboadKPI 
INNER JOIN tblKPIData 
ON tblDashboadKPI.KPI_Id = tblKPIData.KPI_id 
WHERE (((tblKPIData.FinancialMonth)="nov") 
     AND ((tblKPIData.FinancialYear)=2016) 
     AND ((tblKPIData.ImportTimestamp)=(select max(d2.ImportTimestamp) 
              from tblKPIData as d2 
              where d2.KPI_Id = tblKPIData.KPI_Id)) 
     AND ((tblDashboadKPI.Dashboard_Id)=5)); 

thanx您的幫助!