+ Reply to Thread
Results 1 to 4 of 4

SQL Excel Access : Return top 2 from each date in range

  1. #1
    Registered User
    Join Date
    12-27-2015
    Location
    Iowa, USA
    MS-Off Ver
    2010
    Posts
    83

    SQL Excel Access : Return top 2 from each date in range

    Maybe someone can help me with this SQL problem. The user app is Excel, data in back end Access table. To simplify, I have a table "T1" with a date field, a category field, and a count field. Sample might look like this...

    Date Cat Ct
    4/2/19 Blue 5
    4/2/19 Black 8
    4/2/19 Green 3
    4/2/19 Red 10
    4/3/19 Black 9
    4/3/19 White 5
    ...and so on with several of the same date. The question is what SQL statement is needed to pull the top 2 categories (highest count) from each date within a date range. Dates in the table span 1/1/2018 to 12/31/19 and any range within could be selected by the user. The output might look like this....

    Date Cat Ct
    4/2/19 Red 10
    4/2/19 Black 8
    4/3/19 Black 9
    4/3/19 etc
    4/4/19 etc
    4/4/19 etc
    etc

    I'm relatively new to SQL but I've seen a reference to "partition", but I'm not sure that works with Excel/Access, and I haven't found an equivalent (in my brief search for the solution).

    Nevertheless, your help would be great.

    Lewis

  2. #2
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: SQL Excel Access : Return top 2 from each date in range

    Access SQL makes this somewhat painful as it's so limited; I think you need a unique identifier for each row (so probably the PK if that is all in the same table - though I hope not!)

    Please Login or Register  to view this content.
    I think that should be valid in Access, there's a demo of it here I've built for you - though it uses SQL server http://www.sqlfiddle.com/#!18/e2414/1
    Last edited by Kyle123; 06-04-2019 at 05:38 AM.

  3. #3
    Registered User
    Join Date
    12-27-2015
    Location
    Iowa, USA
    MS-Off Ver
    2010
    Posts
    83

    Re: SQL Excel Access : Return top 2 from each date in range

    Kyle, thanks for the reply. It provided enough insight to find and apply the solution. Here's the resulting SQL I used in the Excel/Access environment.

    HTML Code: 
    Thank you so much for spending the time on this for me.
    Lewis

  4. #4
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: SQL Excel Access : Return top 2 from each date in range

    No problem, glad you got it sorted

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. List of Access database paths in Excel - Need to return Access version
    By Wedge120 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-29-2019, 12:19 PM
  2. Replies: 4
    Last Post: 03-21-2018, 09:25 AM
  3. [SOLVED] Excel VBA 2016 32bit - Vlookup Date after date typed in and return value from named range
    By theshybutterfly in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-27-2017, 02:34 AM
  4. Excel 2013 - if date is found between date range return value
    By theshybutterfly in forum Excel General
    Replies: 4
    Last Post: 12-11-2016, 01:14 AM
  5. Check a date range and return value based on the date range
    By KeithCar in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-13-2015, 12:55 PM
  6. Search date range +/- 7 days and return date that falls within range
    By tlafferty in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 07-13-2013, 03:26 PM
  7. excel - access query - date range
    By mxp in forum Excel General
    Replies: 1
    Last Post: 01-31-2006, 11:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1