Results 1 to 7 of 7
  1. #1
    gauravnagpal is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    2

    Grouping Columns with dates

    Hi
    I am working on a very large table that i have to group by Min & Max Dates.
    ColumnA ColumnB
    1-Jan Melbourne
    2-Jan Melbourne
    3-Jan Melbourne


    4-Jan Melbourne
    5-Jan Sydney
    6-Jan Sydney
    7-Jan Sydney
    8-Jan Sydney
    9-Jan Sydney
    10-Jan Sydney
    11-Jan Sydney
    12-Jan Melbourne
    13-Jan Melbourne
    14-Jan Melbourne
    15-Jan Melbourne
    I want the above table to provide an output as below:
    Min Max City
    1-Jan 4-Jan Melbourne
    5-Jan 11-Jan Sydney
    12-Jan 15-Jan Melbourne
    i have done this in excel by sorting dates and then comparing values in Column B
    any help will be greatly appreciated

  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
    Try

    SELECT ColumnB, Min(ColumnA) As MinDate, Max(ColumnA) As MaxDate
    FROM TableName
    GROUP BY ColumnB
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    I can’t see a way to do this without a third column that can be used to group on. If you add a field to the table called ColumnC, then the Sub below would populate ColumnC with the data required to make this query work

    SQL for query
    Code:
    SELECT Min(TableName.ColumnA) AS MinDate, Max(TableName.ColumnA) AS MaxDate, TableName.ColumnB
    FROM TableName
    GROUP BY TableName.ColumnB, TableName.ColumnC;
    Sub
    Code:
    Public Sub AddValueToField()
    Dim Rec As DAO.Recordset
    Dim strSQL As String
    Dim strPrevTown As String
    Dim i As Integer
    strSQL = "SELECT TableName.ColumnA, TableName.ColumnB, TableName.ColumnC " & _
    "FROM TableName " & _
    "ORDER BY TableName.ColumnA;"
    Set Rec = CurrentDb.OpenRecordset(strSQL)
       With Rec
          .MoveFirst
          strPrevTown = .Fields("ColumnB")
          Do Until .EOF
             .Edit
             If strPrevTown = .Fields("ColumnB") Then
                .Fields("ColumnC") = i
             Else
                i = i + 1
                .Fields("ColumnC") = i
             End If
             .Update
             strPrevTown = .Fields("ColumnB")
             .MoveNext
          Loop
       End With
    End Sub
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  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
    It appears I didn't look closely enough at the example.

    As Bob said, you need a way of differentiating the multiple groupings of the same city, so give his code a whirl.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    Quote Originally Posted by pbaldy View Post
    It appears I didn't look closely enough at the example.
    Don't bang the head too hard Paul. From what I've seen of your replies, and I've learnt a lot from many of them, it's not often that you miss something.
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  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
    You're too kind. The brain cramps are coming faster now, so I think senility is right around the corner. Wait, no, I'm going to blame it on being on an iPad and it was bedtime. Yeah, that's it!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    gauravnagpal is offline Novice
    Windows Vista Access 2007
    Join Date
    Apr 2012
    Posts
    2
    Thanks Bob. That worked like a charm
    Regards
    gaurav



    Quote Originally Posted by Bob Fitz View Post
    I can’t see a way to do this without a third column that can be used to group on. If you add a field to the table called ColumnC, then the Sub below would populate ColumnC with the data required to make this query work

    SQL for query
    Code:
    SELECT Min(TableName.ColumnA) AS MinDate, Max(TableName.ColumnA) AS MaxDate, TableName.ColumnB
    FROM TableName
    GROUP BY TableName.ColumnB, TableName.ColumnC;
    Sub
    Code:
    Public Sub AddValueToField()
    Dim Rec As DAO.Recordset
    Dim strSQL As String
    Dim strPrevTown As String
    Dim i As Integer
    strSQL = "SELECT TableName.ColumnA, TableName.ColumnB, TableName.ColumnC " & _
    "FROM TableName " & _
    "ORDER BY TableName.ColumnA;"
    Set Rec = CurrentDb.OpenRecordset(strSQL)
       With Rec
          .MoveFirst
          strPrevTown = .Fields("ColumnB")
          Do Until .EOF
             .Edit
             If strPrevTown = .Fields("ColumnB") Then
                .Fields("ColumnC") = i
             Else
                i = i + 1
                .Fields("ColumnC") = i
             End If
             .Update
             strPrevTown = .Fields("ColumnB")
             .MoveNext
          Loop
       End With
    End Sub

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

Similar Threads

  1. Query input dates for range of dates in two fields
    By kagoodwin13 in forum Queries
    Replies: 3
    Last Post: 02-13-2012, 04:25 PM
  2. Replies: 10
    Last Post: 11-16-2011, 10:58 AM
  3. Replies: 1
    Last Post: 06-09-2011, 09:15 AM
  4. Grouping and Transapose rows with columns
    By HendriX99 in forum Queries
    Replies: 3
    Last Post: 03-14-2011, 03:23 PM
  5. Replies: 3
    Last Post: 11-19-2009, 09:15 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