Results 1 to 4 of 4
  1. #1
    ncub is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2015
    Posts
    10

    Group by, sort then add field based on counter or group

    Just for example, I have a table where all records are un-grouped and un-sorted :

    field_1 field_2 field_3
    ---------------------------
    group_1 rec_1 aaaa
    group_2 rec_1 eeee
    group_3 rec_1 hhhh
    group_1 rec_2 bbbb
    group_1 rec_4 dddd
    group_2 rec_2 kkkk
    group_1 rec_3 cccc
    group_2 rec_3 gggg
    group_3 rec_3 nnnn
    group_3 rec_2 oooo
    group_3 rec_4 ssss


    First I want it grouped by Field_1, then sort based on field_2 in each group.


    After that, add a new field with value which will be change sequentially if met any of these condition :
    for every 3 records if still in a same group
    OR
    when the group change

    So it will become like this

    field_1 field_2 field_3 field_4
    ------------------------------------
    group_1 rec_1 aaaa 1
    group_1 rec_2 bbbb 1
    group_1 rec_3 cccc 1
    group_1 rec_4 dddd 2
    group_2 rec_1 eeee 3
    group_2 rec_2 kkkk 3
    group_2 rec_3 gggg 3
    group_3 rec_1 hhhh 4
    group_3 rec_2 oooo 4
    group_3 rec_3 nnnn 4
    group_3 rec_4 ssss 5

    How to get that result with query in Access 2007 ?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    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.

  3. #3
    ncub is offline Novice
    Windows XP Access 2007
    Join Date
    Apr 2015
    Posts
    10
    I've read but still don't know how to implement it for my problem

    Quote Originally Posted by June7 View Post

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Oh, just hit me - 'for every 3 records' as well as conditional on the group. That is unusual and I expect very difficult. Means first calculating a field that does a sequential count within each group - which can be done with a DCount(). Then calculate another field for the conditional numbering. Whether those two calcs can be combined into one I am not sure.

    Domain aggregate functions can perform slowly in queries.

    Alternatively, write complex VBA code that populates field in 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.

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

Similar Threads

  1. Using an option group to sort by VBA
    By derek7467 in forum Programming
    Replies: 3
    Last Post: 02-19-2014, 09:34 AM
  2. Replies: 3
    Last Post: 06-28-2013, 01:38 PM
  3. Group by one field, sort by another
    By tylerg11 in forum Reports
    Replies: 4
    Last Post: 03-05-2013, 01:17 PM
  4. Replies: 2
    Last Post: 10-18-2012, 06:56 PM
  5. Group Header and then a Sort
    By bbulla in forum Reports
    Replies: 1
    Last Post: 09-29-2011, 09:56 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