Results 1 to 6 of 6
  1. #1
    fabilewk is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    7

    Exclamation TOP clause not working

    I'm using this query:

    SELECT Stats.[userid] AS User, Count(Stats.[userid]) AS Docs
    FROM (SELECT Stats.[userid], First(Stats.[document]) AS FirstOfDocument FROM Stats WHERE GROUP BY Stats.[userid], Stats.[document]) AS Query1
    GROUP BY Stats.[userid]
    ORDER BY Count(Stats.[userid]) DESC;



    but what I actually need is just the top 20 userid, so I added the TOP 20 clause:

    SELECT TOP 20 Stats.[userid] AS User, Count(Stats.[userid]) AS Docs
    FROM....

    but this isn't working. Any ideas?
    Any help would be appreciated.

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Code:
    SELECT Stats.[userid] AS User, Count(Stats.[userid]) AS Docs
    FROM (SELECT Stats.[userid], First(Stats.[document]) AS FirstOfDocument   FROM Stats GROUP BY Stats.[userid], Stats.[document])  AS Query1
    GROUP BY Stats.[userid]
    ORDER BY Count(Stats.[userid]) DESC;
    I took out the word WHERE from your statement and mine worked. Not sure why you did your query this way but this is simpler and it works the same way.

    Code:
    SELECT TOP 20 Stats.UserID, Count(Stats.Document) AS TotalDocs
    FROM Stats
    GROUP BY Stats.UserID
    ORDER BY Count(Stats.Document) DESC;

  3. #3
    fabilewk is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    7

    Correction

    Excuse me, but que query would be something like this:

    SELECT Stats.[userid] AS User, Count(Stats.[userid]) AS Docs
    FROM (
    SELECT Stats.[userid], First(Stats.[document]) AS FirstOfDocument
    FROM Stats
    WHERE
    (Stats.[document] LIKE '*.pdf' OR Estadísticas.[document] LIKE '*.doc' OR Stats.[document] LIKE '*.xls')
    AND Stats.[document] NOT LIKE 'DocumentX' AND
    Stats.[document] NOT LIKE 'DocumentY' AND
    Stats.[document] NOT LIKE 'SomethingZ' AND
    Stats.[userid] NOT LIKE 'somebodyX' AND
    Stats.[userid] NOT LIKE 'somebodyY' AND
    Stats.[userid] NOT LIKE 'somebodyZ'
    GROUP BY Stats.[userid], Stats.[document]) AS Query1
    GROUP BY Stats.[userid]
    ORDER BY Count(Stats.[userid]) DESC;

    Thanks for your help

  4. #4
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I don't understand this question if there is one.

  5. #5
    fabilewk is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Jul 2011
    Posts
    7
    The question is in the first post.
    I'm using this query:

    SELECT Stats.[userid] AS User, Count(Stats.[userid]) AS Docs
    FROM (
    SELECT Stats.[userid], First(Stats.[document]) AS FirstOfDocument
    FROM Stats
    WHERE
    (Stats.[document] LIKE '*.pdf' OR Estadísticas.[document] LIKE '*.doc' OR Stats.[document] LIKE '*.xls')
    AND Stats.[document] NOT LIKE 'DocumentX' AND
    Stats.[document] NOT LIKE 'DocumentY' AND
    Stats.[document] NOT LIKE 'SomethingZ' AND
    Stats.[userid] NOT LIKE 'somebodyX' AND
    Stats.[userid] NOT LIKE 'somebodyY' AND
    Stats.[userid] NOT LIKE 'somebodyZ'
    GROUP BY Stats.[userid], Stats.[document]) AS Query1
    GROUP BY Stats.[userid]
    ORDER BY Count(Stats.[userid]) DESC;

    but I need just the top 20 userid.
    When I added the TOP clause

    SELECT TOP 20 Stats.[userid] AS User, Count(Stats.[userid]) AS Docs
    FROM (....


    the query returned 32 records.

    I would like to know why the top 20 clause is ignored.
    Thanks

  6. #6
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    I think the problem you're encountering is that the TOP X sql statement will return all ties

    So let's say you have 2 matching documents for the last person on your TOP list, and there are 50 people with 2 documents it's going to show all of those because they match your lowest TOP value.

    If you want to limit it to the last USERID it finds with ties then you have to modify your ORDER BY statement to include the USERID field (i.e. sort by the number of documents first, then by the USERID) Just be aware that all ties will not show up and you are always going to favor people with a lower USERID.

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

Similar Threads

  1. Using the TOP clause
    By WSlepecki in forum Queries
    Replies: 1
    Last Post: 04-08-2011, 06:59 AM
  2. Where clause
    By Amerigo in forum Queries
    Replies: 2
    Last Post: 03-30-2011, 07:34 AM
  3. Report "WHERE" clause not working
    By rsearing in forum Reports
    Replies: 4
    Last Post: 05-26-2009, 02:07 PM
  4. IIF clause
    By Peljo in forum Queries
    Replies: 2
    Last Post: 02-05-2008, 11:22 AM
  5. Help on WHERE clause
    By QBCM in forum Programming
    Replies: 1
    Last Post: 12-19-2005, 08:43 PM

Tags for this Thread

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