Results 1 to 5 of 5
  1. #1
    Dev is offline Novice
    Windows XP Access 2000
    Join Date
    Sep 2010
    Posts
    3

    Creating a Grouping query

    Hi everyone,
    I was wondering if someone can help me out in creating a query or new table. I have 2 columns from the same table and one column has many of the same numbers but the other column has unique values. I want to group the repeating number and insert a new field containing all the unique numbers associated with the repeating one.
    here is the sample:

    Number1 Number2
    0168 0165
    0171 0165
    0420 0165
    0148 0168
    0138 0168
    0137 0168
    0136 0168
    0418 0419
    0052 0419
    etc.

    here is what I would like the output to be

    Number2 Repeating Numbers


    0165 0168, 0171, 0420
    0168 0148, 0138, 0137, 0136
    0419 0418, 0052
    Last edited by Dev; 09-01-2010 at 11:20 AM. Reason: easier to understand

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    what you want is better done in excel because you're trying to output horizontally. Access isn't designed for this, as it is designed for vertical output of data in the form of records. It is easier to work with in this sense anyway.

    But ... if vertical with satisfy you, try this for a query:
    Code:
     select distinct col1, col2
    
    from table
    
    where dcount("col2", "table") > 1
    this will give something like:

    Code:
    0165 | 0168
    0165 | 0171
    0165 | 0420
    this is extremely better than columns because the only way to get values out of columns is to use internal functions or loops in UDF'S. This way gives you the option to refer to one field only to get these values out when you need them.
    Last edited by ajetrumpet; 09-01-2010 at 12:27 PM.

  3. #3
    Dev is offline Novice
    Windows XP Access 2000
    Join Date
    Sep 2010
    Posts
    3
    Thanks for the input...
    Any ideas on how I can get my desired output in excel then??
    I would really like the repeating numbers to appear once with the list of all unique values beside it in the same row.

    Thanks again,

    Devo

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    I am not good with excel interfaces at all, but I can write the code. E.G. - if I have a table that is two columns and starts in col A (cells start in A1, duplicate col is col A, related nums are in col B), I write this to transform it:
    Code:
    Option Explicit
    Option Base 1
    
    Sub trans()
    
    Dim i As Long
    Dim r As Range
    Dim r2 As Range
    Dim curVal As String
    Dim NewListPos As Long 'record num
    Dim NewListCol As Long
    Dim usedVals() As String
    
    NewListPos = 0
    NewListCol = 0
    
    'loop col1 for distinct values and pull related col2
    For Each r In Range("a1", Range("a1").End(xlDown))
    
    i = 1
    curVal = r
    
    'if this is first num in list, resize array and move on
    If r.Address = "$A$1" Then
       ReDim Preserve usedVals(1)
       usedVals(1) = r
          GoTo Midline
    End If
       
    
       For i = LBound(usedVals) To UBound(usedVals)
          
          'have we used this num as a ref before?
          If curVal = usedVals(i) Then
             GoTo NextLoop
          End If
       
       Next i
    
    'if new num, resize array
    ReDim Preserve usedVals(UBound(usedVals) + 1)
    usedVals(UBound(usedVals)) = curVal
    
    Midline:
    
    'if new num, start a new transformed row
    NewListPos = NewListPos + 1
    Range("D" & CStr(NewListPos)) = r
    
    'set up for first piece of data
    NewListCol = 1
    
          'if new num, pull all nums in col2
          For Each r2 In Range("a1", Range("a1").End(xlDown))
          
             If r2 = curVal Then
                Range("D" & CStr(NewListPos)).Offset(0, NewListCol) = r2.Offset(0, 1)
                NewListCol = NewListCol + 1
             End If
          
          Next r2
    
    NextLoop:
    
    Next r
    
    End Sub

  5. #5
    Dev is offline Novice
    Windows XP Access 2000
    Join Date
    Sep 2010
    Posts
    3

    Thanks

    Thanks again for your solutions.

    Devo

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

Similar Threads

  1. Grouping hours query
    By DMP84 in forum Queries
    Replies: 0
    Last Post: 08-26-2010, 07:15 AM
  2. Grouping query
    By Mphiri in forum Programming
    Replies: 10
    Last Post: 06-15-2010, 08:58 AM
  3. Creating input box for query
    By dcecil in forum Queries
    Replies: 1
    Last Post: 06-23-2009, 10:08 AM
  4. Query Using Grouping is Cutting off Text
    By tigers in forum Queries
    Replies: 3
    Last Post: 06-22-2009, 11:11 AM
  5. Creating INT function within a Query
    By LCorrieri in forum Queries
    Replies: 3
    Last Post: 01-25-2008, 10:38 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