简体   繁体   中英

overflow error of sql query on MS Access

I need to find max value of a column for a table on MS Access by sql query.

SELECT max( v_difference ) AS max_v_difference
FROM 
(
   SELECT *, vv1 - vv2 AS  v_difference , 
   FROM
   (
     SELECT table3.*  , table1.v1 AS vv1, table2.v1 AS vv2 
     FROM table1, table2, table3
     where table1.id = table2.id and table1.id <> "" and table3.id = table1.id
  )
 ) 

I got error: "overflow"

Any help would be appreciated.

thanks

I'm guessing it has to do with the fact that you are performing implicit cross joins with your innermost subquery. While some SQL engines will optimize those types of queries automatically, MS Access is not one of them.

A cross join returns the Cartesian product of two tables; the Cartesian product is a combination of every row from one table combined with every row from another table. So if table1 has 1,000 rows and table2 has 1,000 rows then the Cartesian product of those tables has 1,000 x 1,000 = 1,000,000 rows.

The situation gets worse quickly as you add tables. If your table3 has 10,000 rows, then the Cartesian product of all three tables is 1,000 x 1,000 x 10,0000 = 10,000,000,000 rows. You can see how combining even modestly sized tables could quickly overwhelm system resources and result in an overflow error.

When you do an INNER JOIN, the resulting rowset is the intersection of the tables where the specified JOIN condition is met. This (almost*) always results in a smaller result set than a CROSS JOIN.

You should use INNER JOINs instead. Try the following:

SELECT max( v_difference ) AS max_v_difference
FROM 
(
   SELECT vv1 - vv2 AS  v_difference
   FROM
   (
     SELECT t1.v1 AS vv1, t2.v1 AS vv2 
     FROM (table1 AS t1 INNER JOIN table2 AS t2 ON t1.id = t2.id)
           INNER JOIN table3 AS t3 ON t1.id = t3.id
     WHERE t1.ID <> ""
  )
 ) 

* It is possible to specify a join condition that will always evaluate to TRUE for every combination of rows. In such a case the result of the INNER JOIN would be the same as the CROSS JOIN. Of course, such a query would have no real-world value, other than to discredit explanations that use unconditionally absolute language, for example, "always" ;).

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