Results 1 to 10 of 10
  1. #1
    mpreston14 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Apr 2013
    Posts
    41

    Greater than expression not working

    Hello,



    I am trying to compare two numbers that are in two different columns in a query to decide which is the largest of the two. The two columns pull from separate tables.

    Column 1: AR1 Points: IIf(IsNull([High AR Points]),0,[High AR Points])

    Column 2: AR2 Points: IIf(IsNull([MaxOfPoints]),0,[MaxofPoints])

    The third column is where I want it to select which is higher.
    Column 3: Which AR is Higher?: IIf([High AR Points]>[MaxOfPoints],"AR1","AR2")

    The issue I am having is that when the "IsNull" kicks in and puts a zero (0) in column 2, column 3 always selects "AR2" even if column 1 has a number.

    Now it also always selects "AR2" when there is a tie, but I am less troubled with that than the above.

    Your guidance is appreciated!

    Mike

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You may have to employ a subquery and retrieve the data for your two IIf alias' before you can aggregate your >

  3. #3
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    you've used isnull for the first two columns, but not the third. try

    Column 3: Which AR is Higher?: IIf([AR1 Points]>[AR2 Points],"AR1","AR2")

    also consider using the nz function for faster operation

    Column 1: AR1 Points: nz([High AR Points],0)

    Column 2: AR2 Points: nz([MaxOfPoints],0)

    The third column is where I want it to select which is higher.
    Column 3: Which AR is Higher?: IIf([High AR Points]>[MaxOfPoints],"AR1","AR2")

  4. #4
    mpreston14 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Apr 2013
    Posts
    41
    Thanks, I tried IsNull on the third column it sort of worked, but it didn't fix when AR2 was legitimately smaller than AR1:
    ID AR1 Points AR2 Points Which AR is Higher?
    1 7.5 0 AR1
    2 25 17.5 AR2
    3 0 5.5 AR2
    4 0 5 AR2
    5 0 0 AR2
    6 12.5 37.5 AR2
    8 12.5 5.5 AR2
    10 12.5 12.5 AR2
    11 5 3.75 AR2
    12 0 0 AR2

    Here is the SQL:

    SELECT Referee_T.ID, IIf(IsNull([High AR Points]),0,[High AR Points]) AS [AR1 Points], IIf(IsNull([MaxOfPoints]),0,[MaxofPoints]) AS [AR2 Points], IIf(IsNull([High AR Points])>IsNull([MaxOfPoints]),"AR1","AR2") AS [Which AR is Higher?]
    FROM ([Temp 3 Highest Point Match as AR1 DETAIL] RIGHT JOIN ([Temp 3 Highest Point Match as AR2 DETAIL] RIGHT JOIN Referee_T ON [Temp 3 Highest Point Match as AR2 DETAIL].ID = Referee_T.ID) ON [Temp 3 Highest Point Match as AR1 DETAIL].RefID = Referee_T.ID) INNER JOIN Officiated_Matches_T ON Referee_T.ID = Officiated_Matches_T.RefID
    GROUP BY Referee_T.ID, IIf(IsNull([High AR Points]),0,[High AR Points]), IIf(IsNull([MaxOfPoints]),0,[MaxofPoints]), IIf(IsNull([High AR Points])>IsNull([MaxOfPoints]),"AR1","AR2");

  5. #5
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    this

    IIf(IsNull([High AR Points])>IsNull([MaxOfPoints]),"AR1","AR2")

    is not what I posted which is

    IIf([AR1 Points]>[AR2 Points],"AR1","AR2")

  6. #6
    mpreston14 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Apr 2013
    Posts
    41
    [AR1 Points] and [AR2 Points] return selection boxes when I run the query.

  7. #7
    mpreston14 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Apr 2013
    Posts
    41
    Well, I figured I did something wrong when the boxes popped up, but looking at it now, it does show the correct choice! Thank you!

  8. #8
    mpreston14 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Apr 2013
    Posts
    41
    Now one follow up question for the ties. If I wanted it to pick the AR1 over the AR2 if there was a tie, I assume a nested IIf would be in order?

  9. #9
    mpreston14 is offline Advanced Beginner
    Windows XP Access 2007
    Join Date
    Apr 2013
    Posts
    41
    This worked: test2: IIf([AR1 Points]>[AR2 Points],"AR1",IIf([AR2 Points]>[AR1 Points],"AR2",IIf([AR1 Points]=[AR2 Points],"AR1")))

  10. #10
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    If I wanted it to pick the AR1 over the AR2 if there was a tie
    use

    IIf([AR1 Points]>=[AR2 Points],"AR1","AR2")

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 03-12-2015, 05:13 AM
  2. DSUM Where Expression not working
    By GraeagleBill in forum Programming
    Replies: 3
    Last Post: 02-23-2015, 09:41 PM
  3. Replies: 11
    Last Post: 04-11-2014, 04:26 PM
  4. Replies: 5
    Last Post: 03-21-2013, 04:37 PM
  5. Expression not working in query
    By Bob Blooms in forum Access
    Replies: 4
    Last Post: 08-25-2012, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums