简体   繁体   中英

SQL Subquery in VB.Net with Access Database

I have developed an SQL query that uses sub-query based on a result set (ie a query queries the results of a sub-query). The query executes as expected in MS Access but will not execute in vb.net (including query builder). The error I receive in query builder is:

Error in WHERE clause near 'ORDER'. Unable to parse query text.

I understand that a typical sub-query selects based on the WHERE clause whereas my SQL is querying based on the FROM clause. Because Access doesn't support the LIMIT command, I can't figure out how structure this SQL so it will run in VB.NET. If the LIMIT command was supported I think I could structure the sub-query properly.

Here is my SQL:

SELECT AVG(Differential) * .96
FROM            
   (SELECT TOP 5 Differential
    FROM            
      (SELECT TOP 10 GameDate, Differential
       FROM ScoringHistory
       WHERE PlayerID = ?
       ORDER BY GameDate DESC)
    ORDER BY Differential ASC, GameDate DESC)

If I change PlayerID = ? to PlayerID = 1 (which is a valid value), query builder gives me the same error as above but I can execute the query in query builder.

I've also tried importing ( ? ) the query from Access into VB.NET and I get the same error.

Any suggestions on how to structure my query so it will execute properly?

Adding more information :

I am implementing via a table adapter. See image. Selecting the query and clicking configure displays the following

Then if I click on the Query Builder button I get this error: Error Message

My code to implement the query looks like this:

        Dim factor As Single
        factor = ScoringHistoryTableAdapter.CalculateFactor(1)

i think you are missing alis name or your subquery

SELECT AVG(Differential) * .96
FROM            
   (SELECT TOP 5 Differential
    FROM            
      (SELECT TOP 10 GameDate, Differential
       FROM ScoringHistory
       WHERE PlayerID = 1
       ORDER BY GameDate DESC) a
    ORDER BY Differential ASC, GameDate DESC) b

It looks like your OrderBy clauses are reversed.

You're ordering by GameDate in the middle From clause, but GameDate isn't included in that Select, only Differential.

Try:

    SELECT AVG(Differential) * .96
    FROM (
          SELECT TOP 5 Differential
          FROM (
                SELECT TOP 10 GameDate, Differential
                FROM ScoringHistory
                WHERE PlayerID = ?
                ORDER BY Differential ASC,GameDate DESC
               )
         ORDER BY Differential DESC
         )

As the original poster I thought I would close the loop for everyone on this.

A little more research indicates that this is a known problem(?) with sub queries and the table adapter wizard. Now knowing this, i learned how to execute SQL the old fashioned way and used ExecuteScalar as Charles had indicated and the SQL works fine.

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