Results 1 to 9 of 9
  1. #1
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591

    SELECT TOP 10 Query returns 12 records

    Here's my SQL



    Code:
    SELECT TOP 10 tbl_ChartData.MACHINE, tbl_ChartData.Shift_Order, tbl_ChartData.DT_Reason_Code, tbl_ChartData.DT_Reason, tbl_ChartData.Shift AS Shift1, Sum(tbl_ChartData.[Count]) AS Data1
    FROM tbl_ChartData
    WHERE (((tbl_ChartData.ProductionDate)>=DateValue([Forms]![frm_Chart]![txt_FromDate])-1+#12/30/1899 23:0:0# And (tbl_ChartData.ProductionDate)<=DateValue([Forms]![frm_Chart]![txt_TillDate])+#12/30/1899 22:59:59#) AND ((tbl_ChartData.MACHINE)=[Forms]![frm_Chart]![cbo_Machine]))
    GROUP BY tbl_ChartData.MACHINE, tbl_ChartData.Shift_Order, tbl_ChartData.DT_Reason_Code, tbl_ChartData.DT_Reason, tbl_ChartData.Shift
    HAVING (((tbl_ChartData.Shift) Like 'A'))
    ORDER BY Sum(tbl_ChartData.[Count]) DESC;
    I've seen this happen a few times recently. I ran this same query earlier this morning and got only 10 records.

    Awaiting your answer.

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If there's a tie, the TOP predicate will return more records. For instance, if I have SELECT TOP 1 but there are 2 records with the same value (in your case count), the query would return 2 records.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    Is there a fix for this? I need to return an exact 10 records for this report to work correctly.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Which record would you want returned in the event of a tie? Adding the appropriate field to the ORDER BY clause should resolve ties and give you exactly 10.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    I have to find some parameters that return extra rows to test this. I'll report back when I get the chance.

    Thanks for the info.

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem. I did a very brief test and it worked for me.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    You diagnosed the problem correctly, but that brought up another problem. For my query to return the top 10 records by Count descending I need the sort. If I remove it, I just get the first 10 records, not the top 10. I see where logic is going to fail because I have three identical values. The only solution I can see is building the data set in VBA/DAO and just stopping when the recordcount reaches 10. For my purposes the bottom ranked record is the least important bit of information.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would still have the same primary sort, but add a secondary sort on a field that would be unique, like an ID field.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    Paul H's Avatar
    Paul H is offline Expert
    Windows XP Access 2010 32bit
    Join Date
    Sep 2011
    Location
    Richmond, VA
    Posts
    591
    Ah, I got it. This thing's been beating me up. I'm ready to move on.

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

Similar Threads

  1. form returns records not in record source
    By aussie92 in forum Forms
    Replies: 2
    Last Post: 01-14-2013, 11:23 AM
  2. Replies: 2
    Last Post: 08-07-2012, 03:58 PM
  3. Delete and/or Select Distinct records query
    By admessing in forum Queries
    Replies: 39
    Last Post: 02-14-2012, 03:50 PM
  4. Replies: 3
    Last Post: 01-04-2011, 07:06 PM
  5. SELECT returns more than one row
    By 83dons in forum Queries
    Replies: 1
    Last Post: 01-13-2010, 11:28 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