+ Reply to Thread
Results 1 to 9 of 9

Thread: Finding the larger of 2 values in a query

  1. #1
    Registered User
    Join Date
    04-21-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    54

    Finding the larger of 2 values in a query

    I have a query base off data from an ODBC source. Unfortunately, they messed with the tables (head office) and now I need to make some changes to my database.

    My issue is that they use 1 of 2 fields now to track Life expectancy dates for equipment. The first (original one) is a scheduled refresh date. The other is a suggested refresh date. Needless to say, I need to poll which value is higher. I'd prefer to make a new field in the query, and have it pick which is the higher between the 2 (creating a new expression field in the query). Is this possible, and how?

  2. #2
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Finding the larger of 2 values in a query

    You can create a custom max function like this:

    SELECT 0.5 * ((sched_refresh + sugg_refresh) + ABS(sched_refresh - sugg_refresh)) AS NewRefresh
    Edit: If numbers are really big, you may pick up rounding errors.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Registered User
    Join Date
    04-21-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    54

    Re: Finding the larger of 2 values in a query

    Quote Originally Posted by davegugg View Post
    You can create a custom max function like this:

    SELECT 0.5 * ((sched_refresh + sugg_refresh) + ABS(sched_refresh - sugg_refresh)) AS NewRefresh
    Edit: If numbers are really big, you may pick up rounding errors.
    Can this be built in the query? (such as an expersion to create a new field)?

  4. #4
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Finding the larger of 2 values in a query

    Yes, you can see that's kind of how I set it up. I used generic field names and no tables since you did not provide any in your original post.

    PS, no need to quote my post, I can scroll up and see it.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  5. #5
    Registered User
    Join Date
    04-21-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    54

    Re: Finding the larger of 2 values in a query

    sorry, habbit. Would this work if one of the fields was null (no data entered). They are date fields, so I think the numbers would be fine?

    Edit:

    Also, I don't think I know where to put the code. A bit new to doing this. Is it put in the Criteria field? or is put in the Field field?
    Last edited by jik_ff; 08-19-2011 at 04:15 PM.

  6. #6
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Finding the larger of 2 values in a query

    I would think it'd work as long as both fields weren't null, but I'd check to be sure. Can you post your current query's sql, then I may be able to give you a better idea of where to put it.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  7. #7
    Registered User
    Join Date
    04-21-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    54

    Re: Finding the larger of 2 values in a query

    I don't think it works with null, (also it doesn't like the SELECT at the front) as when I did this:
    0.5 * (([SCHEDULED_RETURN_DATE] + [SUGGESTED_REFRESH_DATE]) + Abs ([SCHEDULED_RETURN_DATE] - [SUGGESTED_REFRESH_DATE]))

    it came back blank. SCHEDULED_RETURN_DATE is empty in my test value.

    I know it works though cause this:
    0.5 * ((9 + 7) + Abs (9 - 7))
    returns 9.

    (BTW, doing this in Access 2007, not SQL)

  8. #8
    Forum Guru davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2007
    Posts
    1,879

    Re: Finding the larger of 2 values in a query

    Ok, even in Access, there is SQL behind each query. You can change the view for the Query builder to SQL to get what the SQL behind the query is. I'm not sure if you can perform arithmetic on dates, you may have to use date-specific functions. I'll look into it, but probably not before Monday, so you could try Googling it if you want to find an answer before then.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  9. #9
    Registered User
    Join Date
    04-21-2010
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    54

    Re: Finding the larger of 2 values in a query

    Thanks, you gave me a good point to start with. I'll do some searching as well.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0