Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92

    Query to pull a percentage of records by user

    Hello,

    I am trying to create a report for our auditors to use to pull a random selection of records. The real catch is that it needs to randomly pull 5% of each users productionfrom a specified time frame. Pulling the time frame is not an issue but I am not sure where to start for the random pull. Amoung other data my table does have IDs from auto numbering and the user initials which I beleive would be the primary fields for this project.

    Can this be done through Access? If so can someone point me in the right direction? I have seen a number of references for selecting random records but nothing taking it to the level of specifically selecting 5% per user.




    Thanks

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Don't overlook the power of Google,


    Did you look at https://www.accessforums.net/tutorials/selecting-random-top-n-group-41737.html



  3. #3
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92
    Thanks! This looks legit. As for the power of Google.... It failed me. I tried numerous searches before posting my question but never found that post.

  4. #4
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92
    Hello,

    I have tried this formula and it took a bit to get it to not error with my data. However, it is not working as intended. For one rep it pulled 2 records where it should only be 1, for another rep it pulled 14 where it should be 18 and on a 3rd it pulled 20 where it should be 17. Since the number pulled is not consistanly either high or low and because in some instances it is off by more than 1 it does not seem like a simple rounding issue. Does any one have any ideas on what is actually happening or what may be wrong?

    Sample of table/data:

    Table: RandomPull
    Fields: ID, ESR Initials, RandomID
    Data: 94446, LD, 707
    Data: 94439, LD, 3711

    SQL:
    SELECT TOP 5 PERCENT RandomPull.[ESR Initials], RandomPull.ID, RandomPull.RandomID
    FROM RandomPull
    GROUP BY RandomPull.[ESR Initials], RandomPull.ID, RandomPull.RandomID
    ORDER BY RandomPull.RandomID;

    Thanks

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Can you show more data? Post some even if csv.

  6. #6
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92
    AuditData.zip

    Not sure if I did this right so let me know if I need to try it again. Thanks

  7. #7
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    I imported your test data into a Table called Randomm It contains 1475 records..

    Note: I created another Random number called xtrafield which is defined as Number Double. I populated it with the routine in the link to the sample https://www.accessforums.net/tutoria...oup-41737.html

    I then ran this query.
    Code:
    SELECT  Top 5 PerCent Randomm.esrInitials, rid, RandomID,Randomm.xtrafield
    FROM Randomm
    GROUP BY Randomm.esrInitials, rid, RandomId,Randomm.xtrafield
    ORDER BY Randomm.xtrafield;
    It returned 74 records as below. It has returned a random top 5 Percent of your records. 5% of 1475

    esrInitials rid RandomID xtrafield
    CPH 96122 5726 6.90073394775391
    CXS 95526 75 3.15544009208679
    CXS 95994 7801 1.0136604309082
    CXS 95560 4055 15.888744354248
    CXS 95887 864 5.75895309448242
    CXS 96263 6644 6.71076202392578
    CXS 95974 7813 7.27550983428955
    CXS 97130 3136 4.81898212432861
    CXS 96572 5463 4.16645431518555
    CXS 95511 5084 9.35783386230469
    CXS 95877 870 6.96197748184204
    CXS 96000 3798 9.85704612731934
    CXS 97128 1137 11.1604032516479
    CXS 95952 5826 2.75169610977173
    CXS 95856 9882 12.5424919128418
    CXS 95495 9093 14.6378517150879
    CXS 95984 7807 1.48884057998657
    CXS 96261 4645 13.6329708099365
    CXS 96564 7467 2.72394418716431
    CXS 96074 7754 3.32273840904236
    CXS 96699 2388 2.13092565536499
    CXS 95611 5025 0.706744194030762
    CXS 95588 2039 12.0900926589966
    JLC 96663 6409 8.78748893737793
    JLC 95911 4850 2.14503407478333
    JLC 96267 641 7.29514360427856
    JLC 96256 9648 15.0124311447144
    JLC 96253 6649 1.34816765785217
    JLC 97045 8186 4.53363037109375
    JLC 95734 7953 13.6312007904053
    JLC 96860 3294 4.58121299743652E-02
    JLC 95743 6948 6.80270767211914
    LB 95606 28 6.36464977264404
    LD 96259 2646 4.872483253479
    LD 94753 7528 2.68903970718384
    LD 94757 1525 3.94853353500366
    LD 94521 5663 3.65343689918518
    LD 94469 3694 2.85553336143494
    LD 94526 660 2.73209810256958
    LD 94456 701 4.85638999938965
    LD 94470 4693 9.09740924835205
    LD 97083 6164 15.8099412918091
    LD 96355 8590 15.3824329376221
    LD 94481 5687 14.0773410797119
    LD 94461 5699 0.96670389175415
    LD 96590 3452 6.98259449005127
    LD 96782 5340 9.46614170074463
    LD 96792 5334 8.52504348754883
    LD 95936 9835 7.43393898010254
    LD 96422 5551 1.37924551963806
    LD 96750 3358 7.18250846862793
    LD 96412 5556 7.76973342895508
    LW 96289 2628 7.77575969696045
    LW 96684 7397 2.83496975898743
    LW 96281 4633 4.0208101272583
    LW 96068 1758 9.44407558441162
    LW 96677 401 14.2607326507568
    LW 97064 7175 14.5131826400757
    LW 97060 3177 8.10556411743164
    MC 97162 5117 2.1988570690155
    RPT 96812 5322 5.22921657562256
    SXW 96936 9250 1.28551125526428
    SXW 95617 1022 1.00768804550171
    SXW 97126 9138 0.642675161361694
    SXW 95811 4908 0.814944505691528
    SXW 95636 11 1.97778940200806
    SXW 96968 1231 6.04548454284668
    SXW 97020 3200 6.66330432891846
    SXW 95817 905 8.58444595336914
    SXW 96704 7385 11.2338247299194
    SXW 96227 665 13.5934886932373
    SXW 96513 6497 14.2360744476318
    SXW 96843 6304 14.7644157409668
    SXW 96687 395 6.5826416015625

  8. #8
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92
    Thanks for the help but your results are also incorrect. I am looking for 5% of each ESR's records, not simply 5% of the whole. Example: CXS should have pulled 17 (18 if rounded up) but your results show 22. LW should have 10 to 11 records but only has 7. I am too new to this to tell if the results from that article are truely correct or if it was just a fluke. And if it was correct then why are our results not? This morning I have been trying to incorporate partitions but I keep getting errors so I am not sure that is the right path to go down either.

    I need it to do something like: For each change in [ESR Initials] randomly select 5% of the records.

    Any other suggestions/advice?

    Thanks

  9. #9
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Thanks for the help but your results are also incorrect.
    I think the results are
    not what you expected, but this is a random TOP 5% selection from your data.

    These are your original Counts by ESR Group. What exactly did you want to do? -especially with those groups having 1 or 2 records? Do you want to ignore any group with less than 20 , 25 or what algorithm?

    ESRInitials CountOfrID
    AKB 1
    BJM 2
    BMK 1
    CPH 11
    CXS 343
    CZH 4
    JD 5
    JDJ 2
    JLC 163
    JS 5
    JSF 1
    KS 6
    LB 4
    LD 362
    LM 2
    LW 207
    MC 2
    MCW 2
    PDB 2
    RG 1
    RPT 2
    SSQ 5
    SXW 340
    TRS 1
    VU 1

  10. #10
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92
    I think I am doing a bad job of explaining what my goal is.

    Imagine that there is a separate Table for each ESR. With that the current code would work being applied to each table individually. After pulling a random 5% selection from each table the results would be combined into a single query.

    I am trying to get those results without the need for building all the extra tables. I am also trying to keep it dynamic so that we do not have to modify the database everytime there is a change to staff.

    The results can be rounded up so that if 5% is less than 1 then it should = 1. Or in ither words: 5% or a minimum of 1, whichever is greater.

    Based on the counts from the file I supplied these are the number of records that should be pulled by ESR.

    ESR Records (Min/Max)
    CXS 17/18
    JLC 8/9
    LD 18/19
    LW 10/11
    SXW 17/17

    All others should have 1.


    Thanks

  11. #11
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Ok. This is via a small procedure.

    ESRInitial rId RandomID xtrafield

    AKB 96321 4610 244.090133666992
    AKB Count: 1
    BJM 95949 2827 151.849746704102
    BJM Count: 1
    BMK 95950 3827 100.151893615723
    BMK Count: 1
    CPH 96122 5726 6.90073394775391
    CPH Count: 1
    CXS 95495 9093 14.6378517150879
    CXS 95500 4090 31.3403434753418
    CXS 95494 8094 59.0409469604492
    CXS 95492 6095 65.2644958496094
    CXS 95503 7089 90.2090530395508
    CXS 95508 2086 121.786003112793
    CXS 95493 7094 136.262619018555
    CXS 95498 2091 155.098434448242
    CXS 95502 6089 157.141555786133
    CXS 95507 1086 185.013122558594
    CXS 95509 3085 211.912933349609
    CXS 95499 3091 224.878479003906
    CXS 95505 9087 245.053237915039
    CXS 95501 5090 245.798736572266
    CXS 95506 87 254.95915222168
    CXS 95497 1092 257.197570800781
    CXS 95496 93 282.301483154297
    CXS 95504 8088 295.968719482422
    CXS Count: 18
    CZH 96322 5609 43.9079933166504
    CZH Count: 1
    JD 95644 8006 115.169563293457
    JD Count: 1
    JDJ 97076 9168 123.373725891113
    JDJ Count: 1
    JLC 95679 2985 17.0142707824707
    JLC 95683 6983 23.5452003479004
    JLC 95681 4984 60.8729286193848
    JLC 95639 3009 103.448173522949
    JLC 95677 987 104.955215454102
    JLC 95680 3985 120.832618713379
    JLC 95676 9987 125.059425354004
    JLC 95678 1986 139.980987548828
    JLC 95682 5984 217.515914916992
    JLC Count: 9
    JS 95645 9005 297.350616455078
    JS Count: 1
    JSF 95635 9011 166.520248413086
    JSF Count: 1
    KS 95933 6837 252.89567565918
    KS Count: 1
    LB 95606 28 6.36464977264404
    LB Count: 1
    LD 94461 5699 0.96670389175415
    LD 94456 701 4.85638999938965
    LD 94442 6710 17.9507904052734
    LD 94445 9708 20.8797817230225
    LD 94444 8709 28.2127437591553
    LD 94459 3700 35.0213356018066
    LD 94441 5710 49.3245735168457
    LD 94451 5704 58.2015037536621
    LD 94447 1707 73.5067596435547
    LD 94440 4711 77.7887649536133
    LD 94455 9702 87.9422988891602
    LD 94446 707 109.803718566895
    LD 94439 3711 115.743324279785
    LD 94452 6704 145.16520690918
    LD 94449 3706 162.84782409668
    LD 94450 4705 211.902542114258
    LD 94457 1701 249.628036499023
    LD 94443 7709 293.621215820313
    LD 94448 2706 297.223083496094
    LD Count: 19
    LM 96544 7479 204.300384521484
    LM Count: 1
    LW 95829 2898 62.4016647338867
    LW 95918 1846 68.2588043212891
    LW 95641 5008 79.9157180786133
    LW 95831 4897 125.816986083984
    LW 95833 6895 141.159408569336
    LW 95926 9841 149.904724121094
    LW 95917 846 162.657775878906
    LW 95830 3897 163.802520751953
    LW 95851 4885 184.329971313477
    LW 95919 2845 246.774322509766
    LW 95832 5896 247.372467041016
    LW Count: 11
    MC 96889 2277 66.4617233276367
    MC Count: 1
    MCW 95614 8024 126.868217468262
    MCW Count: 1
    PDB 95893 6860 266.441009521484
    PDB Count: 1
    RG 96182 5691 139.882659912109
    RG Count: 1
    RPT 95928 1840 119.91194152832
    RPT Count: 1
    SSQ 95929 2839 48.9865226745605
    SSQ Count: 1
    SXW 95617 1022 1.00768804550171
    SXW 95636 11 1.97778940200806
    SXW 95619 3021 20.0485458374023
    SXW 95650 4002 122.298484802246
    SXW 95616 22 126.047714233398
    SXW 95653 7001 133.728866577148
    SXW 95623 7018 141.367431640625
    SXW 95651 5002 173.82763671875
    SXW 95622 6019 176.953994750977
    SXW 95642 6007 192.431015014648
    SXW 95621 5019 210.239517211914
    SXW 95649 3003 230.569839477539
    SXW 95618 2021 248.521560668945
    SXW 95624 8018 260.706481933594
    SXW 95615 9023 263.939239501953
    SXW 95620 4020 269.511779785156
    SXW 95652 6001 295.344909667969
    SXW Count: 17
    TRS 96066 9759 72.0014495849609
    TRS Count: 1
    VU 96455 8531 244.912490844727
    VU Count: 1

    Here is the procedure

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : RandOfEachGroup
    ' Author    : Jack
    ' Date      : 19/03/2014
    ' Purpose   :
    '---------------------------------------------------------------------------------------
    '
    Sub RandOfEachGroup()
    '************          SQL FOR          ********************
    '************  QryRandOrigCountsByGroup ********************
    'SELECT Randomm.ESRInitials, Count(Randomm.rID) AS CountOfrID
    'FROM Randomm
    'GROUP BY Randomm.ESRInitials;
    '
    '***********************************************************
    '
        Dim db As DAo.Database
        Dim rs As DAo.Recordset
        Dim rsList As DAo.Recordset
        Dim i As Integer             'counter of records in group
        Dim sESRInitials As String
        Dim sql As String
    10  On Error GoTo RandOfEachGroup_Error
    
    20  Set db = CurrentDb
    30  Set rs = db.OpenRecordset("QryRandOrigCountsByGroup")
    
    
    40  With rs
    50      Do While Not .EOF
    60          i = 0
    70          sql = "select ESRinitials,rid,xtrafield,randomID from randomm where xtrafield " _
                      & "IN (select top 5 percent xtrafield from Randomm where " _
                      & " ESrInitials = '" & Trim$(!ESRinitials) & "')  order by xtrafield "  'Randomized by group
    
    80          Set rsList = db.OpenRecordset(sql)
    90          Do While Not rsList.EOF
    100             i = i + 1
    
                    'This could be written to a table if needed
    
    110             Debug.Print vbTab & rsList!ESRinitials & "   " & rsList!rid & "  " _
                                & rsList!RandomID & "  " & rsList!xtrafield
    120             sESRInitials = rsList!ESRinitials
    130             rsList.MoveNext
    140         Loop
    
    150         .MoveNext
    160         Debug.Print sESRInitials & "   Count: " & i
    170     Loop
    180 End With
    
    190 On Error GoTo 0
    200 Exit Sub
    
    RandOfEachGroup_Error:
    
    210 MsgBox "Error " & Err.number & " on line " & Erl & " (" & Err.Description & ") in procedure RandOfEachGroup of Module AWF_Related"
    End Sub

  12. #12
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92
    I think your a genius! And I really appreciate the help.

    If you would not mind helping just a little more would you please give me the syntax for populating the results into either a table or a query? I have tried a few things but I am just not getting it. The results that populate in the "Immediate" frame looks great; now I just need to get it somewhere useful to the end users.

    Thanks

  13. #13
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Thanks. I've been called a lot worse.

    I'm in the middle of something at the moment but will adjust so it can be output to a table.
    I'll check back after 7PM tonight.

  14. #14
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    Here is the revised procedure which now includes output to a table. The table is named GroupPCTS
    It is created each time this procedure is executed. If the table already exists, it will be deleted and a new table created.

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : RandOfEachGroup
    ' Author    : Jack
    ' Date      : 19/03/2014
    ' Purpose   : Get Random 5% sample of records by ESRInitials group.
    '             Output the results to a table called GroupPCTS.
    '---------------------------------------------------------------------------------------
    '
    Sub RandOfEachGroup()
          '************          SQL FOR          ********************
          '************  QryRandOrigCountsByGroup ********************
          'SELECT Randomm.ESRInitials, Count(Randomm.rID) AS CountOfrID
          'FROM Randomm
          'GROUP BY Randomm.ESRInitials;
          '
          '***********************************************************
          '
              Dim db As DAO.Database
              Dim rs As DAO.Recordset
              Dim rsList As DAO.Recordset
              Dim rsOut As DAO.Recordset
              Dim i As Integer             'counter of records in group
              Dim sESRInitials As String
              Dim sql As String
              Dim createTableSQL As String  'sql to create a table ** each time program is executed
              Dim deleteTableSQL As String
    
    10        deleteTableSQL = "Drop Table GroupPCTS"
    20        createTableSQL = "CREATE Table GroupPCTs( MyId autoincrement PRIMARY KEY," _
                               & " ESRInits text(20), Rid Long, RandomId Long, xtrafield double);"
    
    30        Set db = CurrentDb
    
              'Table for output of Group percentages
    40        On Error Resume Next                      'prevent error when deleting table
    50        db.Execute deleteTableSQL, dbFailOnError
    60        db.Execute createTableSQL, dbFailOnError
    
    70        On Error GoTo RandOfEachGroup_Error       'reset proper error handling
    
    80        Set rs = db.OpenRecordset("QryRandOrigCountsByGroup")
    90        Set rsOut = db.OpenRecordset("GroupPCTS")
    100       With rs
    110           Do While Not .EOF
    120               i = 0
    130               sql = "select ESRinitials,rid,xtrafield,randomID from randomm where xtrafield " _
                            & "IN (select top 5 percent xtrafield from Randomm where " _
                            & " ESrInitials = '" & Trim$(!ESRinitials) & "')  order by xtrafield "  'Randomized by group
    
    140               Set rsList = db.OpenRecordset(sql)
    150               Do While Not rsList.EOF
    160                   i = i + 1
    
                          'This is being written to a tableGroupPCTS
    170                   rsOut.AddNew
    180                   rsOut!ESRInits = rsList!ESRinitials
    190                   rsOut!rid = rsList!rid
    200                   rsOut!RandomID = rsList!RandomID
    210                   rsOut!xtrafield = rsList!xtrafield
    220                   rsOut.Update
    
            'comment out this debug line if it isn't needed.
    230                   Debug.Print vbTab & rsList!ESRinitials & "   " & rsList!rid & "  " _
                                      & rsList!RandomID & "  " & rsList!xtrafield
    240                   sESRInitials = rsList!ESRinitials
    250                   rsList.MoveNext
    260               Loop
    
    270               .MoveNext
            'this debug line can be commented out if it isn't needed.
    280               Debug.Print sESRInitials & "   Count: " & i
    290           Loop
    300       End With
    
    310       rsOut.Close
    320       rsList.Close
    330       rs.Close
    340       MsgBox " Finished Determining and Creating Table GroupPCTS" & vbCrLf & vbTab & " Random 5 % by Group", vbOKOnly
    350       On Error GoTo 0
    360       Exit Sub
    
    RandOfEachGroup_Error:
    
    370       MsgBox "Error " & Err.number & " on line " & Erl & " (" & Err.Description & ") in procedure RandOfEachGroup of Module AWF_Related"
    End Sub
    Good luck with your project.
    Last edited by orange; 03-20-2014 at 07:31 PM. Reason: spelling

  15. #15
    Xarkath is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Apr 2013
    Location
    Millcreek WA
    Posts
    92
    Hi Orange,

    The examples I found and was trying yesterday were far different than your code so I can see why it was not working for me; appearently I was searching for or at least finding a totally different type of code.

    Your code here is working with one oddity. It is selecting to correct number of records but when writing to the table it is doing it multiple times. For example, it selected 1 record for CPH but wrote it to the table 11 times and it selected 18 records for CXS but wrote each of them to the table 343 times.

    As I was typing that I realized what it is doing but not why. It is writing each record a total number of times equall to the total count of records found per group. I was able to correct that issue by modifying line 20 to:

    20 createTableSQL = "CREATE Table GroupPCTs( MyId autoincrement PRIMARY KEY," _
    & " ESRInits text(20), Rid Long, RandomId Long, xtrafield double, UNIQUE(Rid));"

    and moving line 70 down to line 305.


    Thanks again for all the help; this is a peice of code that is going to be resued in a fair number of my projects so I cannot convey just how greatful I am.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 6
    Last Post: 02-23-2014, 03:54 PM
  2. Replies: 2
    Last Post: 05-08-2013, 03:37 AM
  3. Replies: 1
    Last Post: 01-11-2013, 06:01 PM
  4. Audit Trail - Pull User Name From Table, Office 2010
    By brharrii in forum Programming
    Replies: 2
    Last Post: 06-05-2012, 03:31 PM
  5. Creating a percentage of total records report
    By sai_rlaf in forum Reports
    Replies: 3
    Last Post: 08-12-2011, 11:39 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