Results 1 to 5 of 5
  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727

    distinct query

    Hi all, using access 2003/sql server 2008 I have an access front end linking to a sql back end. Now I need to put a condition to a query and having trouble where to begin. My table consist of 3fields: a list of salestickets, a three digit code and an amount. This table is an weekly excel import. The salesticket needs to be unique but there are times when the excel sheet would have duplicate salesticket numbers with different three digit codes Ex. '56789 DRE' and '56789 CRE'. The users wants to import the salesticket with the largest amount. I was thinking I could use an if statement but the problem is I would not know the largest amount at any given time. Ex. '56789 DRE 300' and '56789 CRE 400'. In this case; I would import the one with 400. This excel sheet can consist of more than 50000 rows so scrubing manually before the import is not an option especially since its automated. I also need the code to be sql server friendly. I started a query but it doesn't work:



    Code:
    Select distinct salesticket, coop, amount
    from tblSalesticket
    where max(amount)
    Some guidance please. Thank you

  2. #2
    Conceptz is offline Novice
    Windows XP Access 2003
    Join Date
    May 2012
    Posts
    4
    Your question seems similar to the one I posted earlier today. I was referred to this link (thanks pbaldy!):
    http://www.baldyweb.com/LastValue.htm

  3. #3
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Yes; it's similar but the import table will be different with each import. So I tried the first query because it may be what I am looking for except I get an error: "You tried to execute a query that does not include the specified expression 'Division' as part of an aggregate function. I have three field and each is important. When I leave off one of the fields, the query returns part of results needed.

    [CODE][SELECT MAX (tblSalesticket.Amount) as MAXAmount, tblSalesticket.Salesticket, tblSalesticket.Division
    FROM tblSalesticket
    Group by tblSalesticket.salesticket/CODE]

  4. #4
    Conceptz is offline Novice
    Windows XP Access 2003
    Join Date
    May 2012
    Posts
    4
    For the example in the link, you'll need both queries to get it to work...

    Call this qry_max
    Code:
    SELECT MAX (tblSalesticket.Amount) as MAXAmount, tblSalesticket.Salesticket
    FROM tblSalesticket
    Group by tblSalesticket.salesticket
    Make another query to retrieve all 3 fields:
    Code:
    Select tblSalesticket.* 
    From tblSalesticket Inner Join qry_max on (tblSalesticket.Amount = qry_max.MAXAmount) AND (tblSalesticket.Salesticket = qry_max.Salesticket)

  5. #5
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Got it! Thanks for all your help.

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

Similar Threads

  1. Query for distinct member number
    By Lauri in forum Queries
    Replies: 1
    Last Post: 08-23-2013, 03:22 AM
  2. Delete and/or Select Distinct records query
    By admessing in forum Queries
    Replies: 39
    Last Post: 02-14-2012, 03:50 PM
  3. distinct query
    By slimjen in forum Queries
    Replies: 3
    Last Post: 02-04-2012, 04:44 PM
  4. Distinct count in a query
    By Fabdav in forum Access
    Replies: 5
    Last Post: 09-14-2011, 04:53 PM
  5. DISTINCT on a Union All query?
    By Robeen in forum Queries
    Replies: 3
    Last Post: 04-27-2011, 10:30 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