Results 1 to 4 of 4
  1. #1
    lokiluke is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    6

    Aggregate function returns duplicate values

    Hi all,



    i have a query:

    Code:
    SELECT tblAssets.AssetID, MAX(tblTests.TestDate)
    FROM tblAssets LEFT JOIN tblTests ON tblAssets.AssetID = tblTests.AssetID
    GROUP BY tblAssets.AssetID;
    which returns the latest date for each asset in tblAssets that is recorded in tblTests. there can be multiple entries in tblTests for each asset in tblAssets linked by AssetID field.

    this all works well (thanks Alan), but now i want to add tblTests.TagNumber to the query. when i add that field to the SELECT line it then returns all the values from tblAssets.

    i have tried using multiple joins but cant seem to get them to work.

    can anyone point me in the right direction.

    cheers,

    luke

  2. #2
    lokiluke is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    6
    i have tried using this join but cant seem to find the reason for the error.

    Code:
    SELECT tblAssets.AssetID, MAX(tblTests.TestDate), tags.TagNumber
    FROM tblTests RIGHT JOIN (tblAssets LEFT JOIN (SELECT tblTests.TagNumber FROM tblTests ) tags ON tblTests.AssetID=tblAssetID) ON tblAssets.AssetID = tblTests.AssetID
    GROUP BY tblAssets.AssetID;
    i get a 'syntax error in JOIN operation'

    any ideas?

    cheers,
    luke

  3. #3
    lokiluke is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    6
    ok i have managed to get it to not return an error but it is still returning multiple entries for each AssetID, am i at least on the right track?


    Code:
    SELECT tblAssets.AssetID, MAX(tblTests.TestDate), tags.TagNumber
    FROM tblTests RIGHT JOIN (tblAssets LEFT JOIN (SELECT tblTests.TagNumber, tblTests.AssetID FROM tblTests) tags ON tags.AssetID=tblAssets.AssetID) ON tblAssets.AssetID = tblTests.AssetID
    GROUP BY tblAssets.AssetID, tags.tagnumber;

  4. #4
    lokiluke is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Sep 2011
    Posts
    6
    Eureka! amazing what can happen when you take a short break

    i have managed to solve it using:

    maxdate is:
    Code:
    SELECT tblAssets.AssetID, MAX(tblTests.TestDate) AS TestDate
    FROM tblAssets LEFT JOIN tblTests ON tblTests.AssetID = tblAssets.AssetID
    GROUP BY tblAssets.AssetID
    Code:
    SELECT maxdate.AssetID, maxdate.TestDate, tblTests.TagNumber
    FROM maxdate LEFT JOIN tblTests ON (tblTests.AssetID=maxdate.AssetID AND tblTests.TestDate=maxdate.TestDate)
    now my question is; is this the best way to do this with two queries? or is there a single query i can use?

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

Similar Threads

  1. Replies: 0
    Last Post: 04-25-2011, 07:58 PM
  2. Replies: 6
    Last Post: 01-07-2011, 12:50 PM
  3. an aggregate function error message
    By newtoAccess in forum Queries
    Replies: 1
    Last Post: 11-27-2010, 05:18 PM
  4. Aggregate Query Returns No Values
    By Xiaoding in forum Queries
    Replies: 6
    Last Post: 03-29-2010, 02:01 PM
  5. Access SQL Query - Aggregate function
    By jack in forum Access
    Replies: 0
    Last Post: 11-10-2009, 08:06 PM

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