Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

How to rank subsets of records in a table?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to rank subsets of records in a table?

    Hi,

    I wonder if anyone could show me in Xbasic how to read through a table such as the example browse attached and rank the record entries by score within exam within date.
    Does it require an additional table for temporary records?
    I have indexed the table as I would require it displayed, as follows: DTOS(DATE)+ALLTRIM(EXAM)+PADL(ALLTRIM(STR(999-SCORE)),3,"0")
    However, I have no idea how to write a batch script that would accomplish what I require with regards to ranking.
    The attached browse also shows in the final colum the ranking I would expect to see after running the script.

    Thanks in advance,

    Paul
    Attached Files
    Last edited by pulsoft; 08-21-2012, 08:39 AM. Reason: Corrected attachment and index definition...

    #2
    Re: How to rank subsets of records in a table?

    Why not use a report, with multiple grouping levels?

    If you must store the subgroup's "rank" in each record, you could use the following sequence:

    pseudocode follows (better to enhance your learning experience!)

    1) change the index spec: the desired sort sequence is by date, then by exam, then by INVERTED score. Why inverted? cause you want the top score (the highest score) to be #1 in each group, right?

    2) open the table, set the revised index primary

    3) fetch the first record

    4) store the date + exam in a variable. This combination identifies the "current" subgroup, right? Let's call it the subgroup spec.

    store the number 1 in a variable. this will be used to track the current rank number

    insert the rank into the current record using a table change method, then increment the rank number

    5) fetch through the records until the combination of date and exam changes. This tells you you've hit the first record in the next group. Make sense? Anyway, along the way, as you fetch through a subgroup change the rank number in each record and increment the rank variable as you go

    6) when a new subgroup is encountered reset the rank var to 1 and set the variable containing the current subgroup spec to match the current subgroup spec; then repeat the dance. i.e. step through the next subgroup changing the rank field as you go, continue til the subgroup spec changes.

    7) stop when you try to fetch past the last record

    8) close the table on the way out.

    This requires using nested loops.

    For lots of examples using nested loops check out the xbasic programming puzzles forum here.

    --tom

    Comment


      #3
      Re: How to rank subsets of records in a table?

      Not sure I see the need for a script. Appears to be a simple update operation for the rank field.

      Update expression

      case(score <=15,1,score<45,2,score<99,.....

      assuming your rankshouldbe for the score of 99 in the example is wrong.
      There can be only one.

      Comment


        #4
        Re: How to rank subsets of records in a table?

        Thanks Tom and Stan, I will read and digest your replies and get back to you if I have any questions.

        I must point out that I rushed putting this example table together and of course forgot that a score of 99 is greater than 80 or 60 or whatever and therefore should be ranked higher, please see revised screen grab and index.

        Stan, the actual requirement I have is not for an exams database and the score field or related value could be one of a significantly larger and unknown range of values. So, if my cursory glance at your reply is correct in assuming you advocate a hard-coded update approach then I don't think that would work.

        Having said that I need to look again at your reply.

        Thanks again,

        Paul

        Comment


          #5
          Re: How to rank subsets of records in a table?

          the score field or related value could be one of a significantly larger and unknown range of values
          You'd still have to have some kind of range of values that correlate with a specific rank. If your range of values and scores is larger than your example why not have a table with fields - upper score and rank. It would need to be in upper score order. Then run an update operation that looks up the person's score in the table and returns the rank.
          There can be only one.

          Comment


            #6
            Re: How to rank subsets of records in a table?

            Many thanks to Tom,

            His logic outline helped me to write a working script - see below - I also attach Tom's version which takes a different approach to the same result.

            Thanks again Tom...

            -------------------------------------------------------------------------------------------------------------------------------
            'Date Created: 21-Aug-2011 02:16:04 PM
            'Last Updated: 21-Aug-2012 06:40:56 PM
            'Created By : Paul

            dim vn_lastscore as n
            dim vn_rank as n = 1
            dim vd_date as d
            dim vc_exam as c
            vn_rank=999999

            'open the demo exams table and set the index
            t = table.open("demoexamanalysis",FILE_RW_SHARED)
            t.index_primary_put("Bydateexamscore")
            t.fetch_first()
            if vn_rank=999999
            vd_date=t.date
            vc_exam=t.exam
            vn_rank=1
            vn_lastscore=t.score
            end if
            select
            case t.date = vd_date .and. t.exam = vc_exam .and. t.score <> vn_lastscore
            vn_rank=vn_rank+1
            vn_lastscore=t.score
            case t.date = vd_date .and. t.exam = vc_exam .and. t.score = vn_lastscore
            vn_rank=vn_rank+0
            case t.date <> vd_date .or. t.exam <> vc_exam
            vd_date=t.date
            vc_exam=t.exam
            vn_lastscore=t.score
            vn_rank=1
            end select
            t.change_begin(.t.)
            t.rank=vn_rank
            t.change_end(.t.)
            t.fetch_next()

            t.close()
            -------------------------------------------------------------------------------------------------------------------------------------------------------
            'Date Created: 21-Aug-2011 02:16:04 PM
            'Last Updated: 21-Aug-2012 06:40:56 PM
            'Created By : Tom Cone

            dim vn_lastscore as n
            dim vn_rank as n = 1
            dim vd_date as d
            dim vc_exam as c

            'open the demo exams table and set the index
            t = table.open("demoexamanalysis",FILE_RW_SHARED)
            t.index_primary_put("Bydateexamscore")
            t.fetch_first()

            vd_date=t.date
            vc_exam=t.exam
            vc_curr_grp = dtos(vd_date) + vc_exam
            vn_lastscore = t.score
            while .not. t.fetch_eof()


            while .not. t.fetch_eof() .and. dtos(t.date) + t.exam = vc_curr_grp
            if vn_lastscore <> t.score then
            vn_rank = vn_rank + 1
            vn_lastscore = t.score
            end if

            t.change_begin()
            t.rank = vn_rank
            t.change_end(.t.)
            t.fetch_next()

            end while
            vn_rank = 0
            vc_curr_grp = dtos(t.date) + t.exam

            end while
            t.close()

            Comment


              #7
              Re: How to rank subsets of records in a table?

              I realised later that my script didn't actually need the second case statement as it is superfluous to requirements...

              Should be:

              ---------------------------------------------------------------------------------------------------------------
              dim vn_lastscore as n
              dim vn_rank as n = 999999
              dim vd_date as d
              dim vc_exam as c

              'open the demo exams table and set the index
              t = table.open("demoexamanalysis",FILE_RW_SHARED)
              t.index_primary_put("Bydateexamscore")
              t.fetch_first()
              if vn_rank=999999
              vd_date=t.date
              vc_exam=t.exam
              vn_rank=1
              vn_lastscore=t.score
              end if
              select
              case t.date = vd_date .and. t.exam = vc_exam .and. t.score <> vn_lastscore
              vn_rank=vn_rank+1
              vn_lastscore=t.score
              case t.date <> vd_date .or. t.exam <> vc_exam
              vd_date=t.date
              vc_exam=t.exam
              vn_lastscore=t.score
              vn_rank=1
              end select
              t.change_begin(.t.)
              t.rank=vn_rank
              t.change_end(.t.)
              t.fetch_next()

              t.close()
              ----------------------------------------------------------------------------------------------------------------------------------

              26 lines for Tom and 28 for me...

              Paul

              Comment


                #8
                Re: How to rank subsets of records in a table?

                Thinking about it, could we start a new forum for real life xbasic table processing examples. Nothing too complex, we don't want an excuse for other seasoned Alpha developers to write our code for us (thanks again Tom), but somewhere that is a good starting point for understanding the basics.
                It took me over an hour yesterday to find in the Alpha documentation how to code the index you wish to process a table on.
                We could maybe use an exams database as the basis for the examples.
                E.G. Tables - Exams Master, Students, Student Disciplines, Discipline Exams, Terms (Semesters I think), Exam Timetable (Schedule), Exam Results, etc.
                Some examples could be creating a schedule for a discipline, determining the average mark for an exam instance, determining the rank against previous exam instances, etc.
                Just a thought,

                Paul

                Comment


                  #9
                  Re: How to rank subsets of records in a table?

                  Paul,
                  I believe that is the intent of the AlphaSport database that Alpha supplies already. It is a reference that is commonly used to direct folks to for examlpes of how certain actions might be achieved.

                  Also,
                  your code seems to have a few irregularities.
                  1) if the first record in the table does not have a rank of 999999, then the variables cd_date, vc_exam and vn_rank will not have values entering the select-case selction, and none of the case statements would be satisfied.
                  2) if the first record in the table does have a rank of 999999, then vc_exam and vn_lastscore are defined in that section and since you have not moved to a different record, re-defining the value of those variables within the select-case section generates no change to the values that the variables already have.
                  3) typically, when looping through a table using t.fetch_first()-t.fetch_next(), this is included within a while .not. t.fetch_eof() and end while statements.
                  Last edited by Mike Wilson; 08-22-2012, 09:50 AM.
                  Mike W
                  __________________________
                  "I rebel in at least small things to express to the world that I have not completely surrendered"

                  Comment


                    #10
                    Re: How to rank subsets of records in a table?

                    Originally posted by Mike Wilson View Post
                    Paul,
                    I believe that is the intent of the AlphaSport database that Alpha supplies already. It is a reference that is commonly used to direct folks to for examlpes of how certain actions might be achieved.

                    Also,
                    your code seems to have a few irregularities.
                    1) if the first record in the table does not have a rank of 999999, then the variables cd_date, vc_exam and vn_rank will not have values entering the select-case selction, and none of the case statements would be satisfied.
                    2) if the first record in the table does have a rank of 999999, then vc_exam and vn_lastscore are defined in that section and since you have not moved to a different record, re-defining the value of those variables within the select-case section generates no change to the values that the variables already have.
                    3) typically, when looping through a table using t.fetch_first()-t.fetch_next(), this is included within a while .not. t.fetch_eof() and end while statements.
                    Hi Mike,

                    Sorry for the delay in answering, have been out all day.

                    I appreciate your comment about Alpha Sports but I was talking about a more accessible and specific set of Xbasic data management examples.
                    I will design some tables, describe some data management examples and script them as best I can. If there are errors in my code I am hoping that Alpha vets here will correct them as I go.
                    These example scripts can then be made available for general reference by any Alpha Xbasic newbie, tagged under relevant categories, for example: read and update single table, read several tables and create spin off table, create summary table, delete records if certain conditions on linked tables exist, etc.

                    With regard to your comment about the script I wrote, I am not sure what you mean. It worked for me with no problems, producing exactly the same results as I expected and exactly the same results that Tom's script produced.

                    Thanks for the comments...

                    Paul

                    Comment


                      #11
                      Re: How to rank subsets of records in a table?

                      Originally posted by pulsoft View Post
                      Thinking about it, could we start a new forum for real life xbasic table processing examples. Nothing too complex, we don't want an excuse for other seasoned Alpha developers to write our code for us (thanks again Tom), but somewhere that is a good starting point for understanding the basics.
                      It took me over an hour yesterday to find in the Alpha documentation how to code the index you wish to process a table on.
                      Paul
                      There's always place here for real life table processing and record manipulation, and lots of tips available. And many ways of achieving the result. I've found out some real nifty things useful for myself. Always willing to share.
                      Now you have found after an hour how to create the code for the desired index. Publish that here, and lets see what it is, and other possible ideas will emerge. Keeps it alive. I am for it.
                      I have been reading, understanding, learning from all of Stan and Tom's replies and comparing to my own thoughts on this thread. I'm trying however not to stand on anyones toes by adding spurious advice - that I tend sometimes to do.

                      Comment


                        #12
                        Re: How to rank subsets of records in a table?

                        Originally posted by Mike Wilson View Post
                        Paul,
                        I believe that is the intent of the AlphaSport database that Alpha supplies already. It is a reference that is commonly used to direct folks to for examlpes of how certain actions might be achieved.

                        Also,
                        your code seems to have a few irregularities.
                        1) if the first record in the table does not have a rank of 999999, then the variables cd_date, vc_exam and vn_rank will not have values entering the select-case selction, and none of the case statements would be satisfied.
                        2) if the first record in the table does have a rank of 999999, then vc_exam and vn_lastscore are defined in that section and since you have not moved to a different record, re-defining the value of those variables within the select-case section generates no change to the values that the variables already have.
                        3) typically, when looping through a table using t.fetch_first()-t.fetch_next(), this is included within a while .not. t.fetch_eof() and end while statements.
                        My Apologies Mike,

                        I copied and pasted the wrong script - the one above has while not EOF omitted... What a dummy... SB:

                        dim vn_lastscore as n
                        dim vn_rank as n
                        dim vd_date as d
                        dim vc_exam as c
                        vn_rank=999999

                        'open the demo exams table and set the index
                        t = table.open("demoexamanalysis",FILE_RW_SHARED)
                        t.index_primary_put("Bydateexamscore")
                        t.fetch_first()
                        while .not. t.fetch_eof()
                        if vn_rank=999999
                        vd_date=t.date
                        vc_exam=t.exam
                        vn_rank=1
                        vn_lastscore=t.score
                        end if
                        select
                        case t.date = vd_date .and. t.exam = vc_exam .and. t.score <> vn_lastscore
                        vn_rank=vn_rank+1
                        vn_lastscore=t.score
                        case t.date = vd_date .and. t.exam = vc_exam .and. t.score = vn_lastscore
                        vn_rank=vn_rank+0
                        case t.date <> vd_date .or. t.exam <> vc_exam
                        vd_date=t.date
                        vc_exam=t.exam
                        vn_lastscore=t.score
                        vn_rank=1
                        end select
                        t.change_begin(.t.)
                        t.rank=vn_rank
                        t.change_end(.t.)
                        t.fetch_next()
                        end while
                        t.close()

                        Paul

                        Comment


                          #13
                          Re: How to rank subsets of records in a table?

                          Hi Ray,

                          The code is in the script:

                          t.index_primary_put("Bydateexamscore")

                          I just made sure that I had a suitable index on the table for processing:
                          DTOS(DATE)+ALLTRIM(EXAM)+PADL(ALLTRIM(STR(999-SCORE)),3,"0")
                          Instead of inverting the score field I subtracted from a hard coded value of 999 (I knew that none of my dummy data exceeded this value)

                          I attach a screen grab of the help page for indicating the primary index for processing.
                          What took me so long was locating this in the first place. I had no idea what tags to look under. This is my main beef with Alpha's help documentation. There is no logical indexing...
                          Hence my suggestion for some form of Xbasic basics forum. I know there is a forum for scripts and another that deals with puzzles, but I do not want to wade through pages of wide ranging topics in the hope of finding something pertinent to a mundane every day task that I haven't tackled in Alpha before...

                          Thanks for your post,

                          Paul
                          Attached Files

                          Comment

                          Working...
                          X