Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8150941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:12:45+00:00 2026-06-06T15:12:45+00:00

Can this MS-Access query be improved? i.e. the SELECT TOP 1 col FROM Table2

  • 0

Can this MS-Access query be improved?

i.e. the “SELECT TOP 1 col FROM Table2” line is repeated

If there is an index before curindex in Table2,
the query returns the previous value of col from Table2,

otherwise the value of col matching id is returned from Table3.

i.e.

if curindex = 7 value returned = 221 from Table2

if curindex = 5 value returned = 200 from Table3

Table2                               Table3
id index  col                        id    col
1    1    110                        1     100
1    2    120                        2     200
1    3    130                        3     300
1    4    140
2    5    211
2    6    221
2    7    231

PARAMETERS [curindex] Short;
SELECT TOP 1
  IIF (
    ( SELECT TOP 1 col
      FROM Table2
      WHERE index < [curindex]
       AND id =
        ( SELECT id
           FROM Table2
           WHERE index = [curindex])
    ) ,
        ( SELECT TOP 1 col
          FROM Table2
          WHERE index < [curindex]
            AND id =
            ( SELECT id
              FROM Table2
              WHERE index = [curindex])
          ORDER BY index DESC
        ),
        ( SELECT TOP 1 col
          FROM Table3
          WHERE id = 
           ( SELECT id
             FROM Table2
             WHERE index = [curindex])
        )
     ) AS col
FROM Table2
ORDER BY index
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:12:47+00:00Added an answer on June 6, 2026 at 3:12 pm

    You can limit the cost of the iff condition by reducing your result query.

    ie

    SELECT TOP 1
      IIF (
    ( SELECT TOP 1 col
      FROM Table2
      WHERE index < [curindex]
       AND id =
        ( SELECT id
           FROM Table2
           WHERE index = [curindex])
    ) ,
        ( SELECT TOP 1 col
          FROM Table2
          WHERE index = [curindex])
        ),
        ( SELECT TOP 1 col
          FROM Table3
          WHERE id = 
           ( SELECT id
             FROM Table2
             WHERE index = [curindex])
        )
     ) AS col
    FROM Table2
    ORDER BY index  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting a syntax error with this access query, but I can't see what
I wonder why I can't execute this SQL query in access 2007 through VBA,
Let's ay I have this query: var results = from row in db.Table select
I have an Ms Access view(query) as following select * from employee Where EmployeeId=SomeID
Protected Means, we can access this member only in a deriving class, and internal
I have a Page with many Comments. Many users can access this page and
How can this code compile? The code below in the operator int CAN access
I have an URL like this /home/action/id How can I access this id in
I can't solve this on MS Access SQL. I have a foo table with
How does this program access other processes memory? How can it write into the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.