Results 1 to 8 of 8
  1. #1
    emihir0 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    22

    Consecutive without recursion.

    I have the following table:

    id | year | team
    1 | 2001 | 1
    2 | 2003 | 1
    3 | 2005 | 2
    4 | 2006 | 1
    5 | 2008 | 3


    6 | 2012 | 1
    7 | 2014 | 1

    And I want to obtain the following:

    id | id
    1 | 2
    6 | 7

    Basically, the touples which are consecutive of team '1'.

    I know how to obtain this:

    id | id
    1 | 2
    2 | 4
    4 | 6
    6 | 7



    which are touples of 2 closest teams of '1'.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    your question makes no sense.

  3. #3
    emihir0 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    22
    I want consecutive pairs of team '1' in my output (where there is no other data between the pair of 2).

    So for example ID 1 and 3 would not qualify, as there is ID 2 between ID1 and 3.

    However, ID6 and 7 would qualify.

    Please bear in mind that ID field is not autoNum field (I made this arbitrarily easier here as an example).

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    How do you obtain that output?

    I suspect will need VBA writing records to a temp table.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    emihir0 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    22
    Input table teams:

    id | year | team

    1 | 2001 | 1
    2 | 2003 | 1
    3 | 2005 | 2
    4 | 2006 | 1
    5 | 2008 | 3
    6 | 2012 | 1
    7 | 2014 | 1


    Output:


    id | id
    1 | 2
    2 | 4
    4 | 6
    6 | 7

    Query:
    Code:
    SELECT t3.id, t4.id 
    FROM 
            teams t3 
                JOIN teams t4 
                    ON t3.team = t4.team AND t3.id <> t4.id
                JOIN (
                    SELECT         t1.team, t1.year AS year1, min(t2.year) AS year2
                    FROM         teams t1 left JOIN teams t2
                                ON t1.team = t2.team AND t1.year < t2.year 
                    WHERE         t1.team = 1 AND t2.team = 1
                    GROUP BY     t1.team,t1.year
                    ) Q1 
                    ON t3.team = q1.team AND t3.year = q1.year1
    WHERE t3.year = q1.year1 AND t4.year = q1.year2;

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    Don't think your second desired output can be done solely with SQL. But did you try adding filter criteria to restrict dataset to just the team records?

    Did you mean 'tuple'?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    emihir0 is offline Novice
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    22
    Quote Originally Posted by June7 View Post
    Don't think your second desired output can be done solely with SQL. But did you try adding filter criteria to restrict dataset to just the team records?

    Did you mean 'tuple'?
    Yes, I meant tuple, sorry.

    If you mean the 4 tuple output as my second desired ouput, I can do it though SQL (as I provided in my post above).

    However, I cannot figure out how to do the first one, with 2 tuples. Logically it should be way easier... maybe I'm just tired today and should attempt it tomorrow again.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,646
    Sorry, got your first and second mixed up. Yes, I expect the more restrictive output would be more difficult.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Consecutive Date Ranges
    By ProwlingCamel in forum Access
    Replies: 6
    Last Post: 09-29-2015, 12:51 AM
  2. Replies: 2
    Last Post: 12-09-2014, 03:32 AM
  3. recursion in Access?
    By DB88 in forum Access
    Replies: 18
    Last Post: 05-21-2014, 01:51 PM
  4. Count Consecutive Only
    By WickidWe in forum Queries
    Replies: 13
    Last Post: 12-16-2013, 02:33 AM
  5. Consecutive ID numbers
    By jdvd in forum Database Design
    Replies: 2
    Last Post: 12-11-2011, 06:48 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