简体   繁体   中英

Convert SQl query to MS Access

SELECT * 
FROM data WHERE (object,TCH_Traffic) IN 
( SELECT object, MAX(TCH_Traffic)
  FROM data
  GROUP BY object
)

Can this above query be modified to run with MS Access database. I am getting error to "revise the select statement of " prompt as attached in MS access 2010 db.

在此处输入图片说明

Just use a correlated subquery:

SELECT * 
FROM data
WHERE TCH_Traffic = (SELECT MAX(d2.TCH_Traffic)
                     FROM data as d2
                     WHERE d2.object = data.object
                    );

MS Access' default Jet/ACE SQL Engine can process a subquery in a WHERE condition using IN clause. However, subquery must return one column value. Consider the following adjustment:

SELECT * 
FROM data WHERE (object) IN 
   ( SELECT object
     FROM data
     GROUP BY object
     HAVING TCH_Traffic = MAX(TCH_Traffic)
   );

However, you can simply use a derived table with INNER JOIN and avoid WHERE clause subquery. Also, same derived table query can be saved as a stored query object in MS Access and explicitly referenced in join clause:

SELECT * 
FROM data     
INNER JOIN
   ( SELECT object, MAX(TCH_Traffic) As maxTraffic
     FROM data
     GROUP BY object
   ) As dT    
ON data.object = dT.object
AND data.TCH_Traffic = dt.maxTraffic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM