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

Update Query for Groups of Records

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

    Update Query for Groups of Records

    Rather than struggle with this all day, I thought I would ask for help from all you smart people.

    I have a table similiar to:
    ItemNo ItemCategory ItemSortNo

    ItemNo is unique, and ItemCategory is repeated several times as a category can have multiple items. Out of say 100 records, 5 are assigned to category 1001. I need to set an incremental value of ItemSortNo for those 5. Then do the same for category 1002, etc. So for each category group, the ItemSortNo starts over again at 1.

    I can update one at a time by setting a filter on that category number. What I need to do is get the list of category numbers and loop over them to perform the operation on the entire table.

    I'm used to SQL where I might query and get a result set of just that column, turn it into an array or list and use that to loop with. However, it appears that in Alpha, you get the entire record, all columns.

    So, how does one perform such an operation as I've laid out?

    #2
    Re: Update Query for Groups of Records

    Originally posted by CDeCinko View Post
    So, how does one perform such an operation as I've laid out?
    By entering a filter expression for the operation.
    There can be only one.

    Comment


      #3
      Re: Update Query for Groups of Records

      Without claiming to be one of the smart people it occurs to me that one solution would be to open an instance of the table with a script. Then sort it based on the ItemCategory field. That can be done using a query. It can also be done using a permanent index.

      Then initialize a counter with the number 1, fetch the first record, assign counter value to the record.

      then fetch the next record, if the category field is the same as the prevoius record, increment counter and assign it to the record, otherwise reset the counter to 1 and assign that value to the record.

      fetch the next record and do the same thing.

      continue until you've processed them all; then close the table


      The xbasic commands you'll need:

      <tbl>.open() and <tbl>.close()
      <tbl>.fetch_first()
      <tbl>.fetch_next()
      while .not. <tbl>.fetch_eof()
      end while
      <tbl>.change_begin()
      <tbl>.change_end()
      <tbl>.index_primary_put() or <tbl>.query_create()

      A few minutes in the xbasic reference and all will become clear.
      Last edited by Tom Cone Jr; 04-30-2008, 01:29 PM.

      Comment


        #4
        Re: Update Query for Groups of Records

        I got that far, but what would expression would I use so it groups by the category?

        Comment


          #5
          Re: Update Query for Groups of Records

          I need to set an incremental value of ItemSortNo for those 5.
          Why?

          Seems redundant to me.

          In order to assign a sort number to the items (whether you do that with a script or black magic) you have to sort those items somehow or decide on how the sort number should be assigned, i.e., sort them.
          If you could sort these items, then why do you need the field ItemSortNo?

          Sounds to me, you simply need either an index, a range or a query with the proper sort order.

          Comment


            #6
            Re: Update Query for Groups of Records

            First, it's not my table so not my decision on having or not having a sort order column. But at some point in time, the record order or item number sort order will not always match the preferred sort order. Today they do only because the data has been recently added. Problem is, some short sighted code caused the sort order to number from 1 to 999 for the entire table instead of within each category. Now I need to correct it.

            Comment


              #7
              Re: Update Query for Groups of Records

              Connie:
              Help me out here because I am still foggy on this:
              If I were to suggest anything, I would probably repeate what Tom has already said. But, to implement that, you have to make a decision on how you are going to order these records in the first place in order to run the script.
              So, let's do this baby steps:
              How are you going to order these records?

              Comment


                #8
                Re: Update Query for Groups of Records

                Order by ItemCategory, then by ItemNo. So initially the sort of ItemNo and ItemSortNo would be the same. Later we'll adjust via a form so they may not be the same.

                Comment


                  #9
                  Re: Update Query for Groups of Records

                  Later we'll adjust via a form so they may not be the same.
                  And what will it be later?
                  I am not sure if I am making my point clear?
                  In order to assign a sort number, whether now or in the future, .. YOU HAVE TO SORT THE RECORDS FIRST... so what's the point of having that number?

                  Granted, you might be abiding by someone else's idiocracy... but in th end, you will have to sort the records in order to give them a sort number.. and since we live in a world mostly run by idiots, I guess you have to add that idiotic sorting field..

                  So, follow what Tom has suggested with the additional step of sorting the records first in order to give them the proper sort order.

                  Sorry if my reaction seems improper, it's just certain things strike me as odd..

                  Comment


                    #10
                    Re: Update Query for Groups of Records

                    I can see a use for a sort_order field and also can see why it makes no difference intially what the order may be. I use just such an order field to manipulate the order of records in more than one of my embedded browses based on one-to-many sets.

                    If this is the case then the category has to be sorted at first but other than that no other sort is necessary as when the sort_order field is created most likely via Tom's code (as that does seem the simplest approach), then the records can be sorted with a script later on. If this sort_order field is to be used to change the order at some time or even many times in the future, then what Connie wants is just a field that will regulate the order of the records...not related to anything else at all.

                    But then maybe I am misinterpreting what Connie's (her boss's use) will be. :)
                    Last edited by MikeC; 04-30-2008, 04:17 PM.
                    Mike
                    __________________________________________
                    It is only when we forget all our learning that we begin to know.
                    It's not what you look at that matters, it's what you see.
                    Henry David Thoreau
                    __________________________________________



                    Comment


                      #11
                      Re: Update Query for Groups of Records

                      Yes, thank you for seeing what I am trying to do. I have some basic code that will number all records. I just need a nudge in where/how to reset the number for each change in the category.

                      Code:
                      dim tbl as p
                      dim shared query as p
                      dim shared query.filter as c
                      dim shared query.order as c
                      dim shared query.options as c
                      dim shared query.description as c
                      dim shared srtCounter as n
                      
                      tbl = table.open("bidassmbly")
                      
                      query.filter = ".t."
                      query.order = "Bidsheet_No"
                      query.options = ""
                      
                      tbl.query_create()
                      
                      srtCounter = 1
                      
                      tbl.fetch_first()
                      while .not. tbl.fetch_eof()
                      	tbl.change_begin()
                      		tbl.__SrtOrderTest = srtCounter
                      	tbl.change_end(.t.)
                      	srtCounter = srtCounter + 1
                      	tbl.fetch_next()
                      end while
                      tbl.close()
                      end
                      Originally posted by MikeC View Post
                      I can see a use for a sort_order field and also can see why it makes no difference intially what the order may be. I use just such an order field to manipulate the order of records in more than one of my embedded browses based on a one-to-many set.

                      If this is the case then the category has to be sorted at first but other than that no other sort is necessary as when the sort_order field is created most likely via Tom's code (as that does seem the simplest approach), then the records can be sorted with a script later on. If this sort_order field is to be used to change the order at some time or even many times in the future, then what Connie wants is just a field that will regulate the order of the records...not related to anything else at all.
                      One use for this sort_order is for displaying in a browse a specific order. For instance, you need to assemble the parts of a sandwich in the correct order reguardless of what order you chose your ingredients.

                      Comment


                        #12
                        Re: Update Query for Groups of Records

                        Connie,
                        My scripting usually gets the job done but is not the "nicest" looking by far!! I think this would work for you, but if others here give another way definitely try theirs too! This can most likely be abbreviated but is much easier to understand when in the form I am giving.

                        The last part of your script add what is in red.

                        Code:
                         
                        [COLOR=red]dim vCategory as c[/COLOR]
                         
                        tbl.fetch_first()
                        [COLOR=red]vCategory=tbl.Bidsheet_No[/COLOR]
                        while .not. tbl.fetch_eof()
                        [COLOR=red]   if vCategory<>tbl.Bidsheet_No[/COLOR]
                        [COLOR=red]     srtCounter=1[/COLOR]
                        [COLOR=red]      tbl.change_begin()[/COLOR]
                        [COLOR=red]      tbl.__SrtOrderTest = srtCounter[/COLOR]
                        [COLOR=red]      tbl.change_end(.t.)[/COLOR]
                        [COLOR=red]      srtCounter = srtCounter + 1[/COLOR]
                        [COLOR=red]      tbl.fetch_next()[/COLOR]
                        [COLOR=red]    else[/COLOR]
                              tbl.change_begin()
                              tbl.__SrtOrderTest = srtCounter
                              tbl.change_end(.t.)
                              srtCounter = srtCounter + 1
                              tbl.fetch_next()
                        [COLOR=red]   end if[/COLOR]
                        end while
                        tbl.close()
                        end
                        Mike
                        __________________________________________
                        It is only when we forget all our learning that we begin to know.
                        It's not what you look at that matters, it's what you see.
                        Henry David Thoreau
                        __________________________________________



                        Comment


                          #13
                          Re: Update Query for Groups of Records

                          try this. This should check the current record category against the previous record category and increment the counter when the category changes.

                          Code:
                          dim tbl as p
                          dim shared query as p
                          dim shared query.filter as c
                          dim shared query.order as c
                          dim shared query.options as c
                          dim shared query.description as c
                          dim shared srtCounter as n
                          dim vCatNow as C
                          dim vCatPast as C
                          
                          tbl = table.open("bidassmbly")
                          
                          query.filter = ".t."
                          query.order = "Bidsheet_No"
                          query.options = ""
                          
                          tbl.query_create()
                          srtCounter = 1
                          
                          tbl.fetch_first()
                          	vCatPast = tbl.category
                          while .not. tbl.fetch_eof()
                          	vCatNow = tbl.category	
                          		if vCatNow <> vCatPast
                          				srtCounter = srtCounter + 1
                          		end if
                          	tbl.change_begin()
                          		tbl.__SrtOrderTest = srtCounter
                          	tbl.change_end(.t.)
                          		vCatPast = vCatNow
                          	tbl.fetch_next()
                          end while
                          tbl.close()
                          end
                          Mike W
                          __________________________
                          "I rebel in at least small things to express to the world that I have not completely surrendered"

                          Comment


                            #14
                            Re: Update Query for Groups of Records

                            Late to the feast but should this portion
                            Code:
                            if vCatNow <> vCatPast
                            	srtCounter = srtCounter + 1
                            end if
                            be

                            Code:
                            if vCatNow <> vCatPast
                                    srtCounter = 1
                            else
                            	srtCounter = srtCounter + 1
                            end if
                            There can be only one.

                            Comment


                              #15
                              Re: Update Query for Groups of Records

                              Yes, folks we finally have a winner! Here is the working code:

                              Code:
                              dim tbl as p
                              dim shared query as p
                              dim shared query.filter as c
                              dim shared query.order as c
                              dim shared query.options as c
                              
                              tbl = table.open("bidassmbly")
                              
                              query.filter = ".t."
                              query.order = "Bidsheet_No"
                              query.options = "M"
                              
                              tbl.query_create()
                              srtCounter = 0
                              
                              tbl.fetch_first()
                              	vBidsheetNoPrev = tbl.Bidsheet_No
                              
                              while .not. tbl.fetch_eof()
                              	vBidsheetNoCur = tbl.Bidsheet_No
                              
                              	if vBidsheetNoCur <> vBidsheetNoPrev
                              		srtCounter = 1
                              	else
                              		srtCounter = srtCounter + 1
                              	end if
                              	
                              	tbl.change_begin()
                              		tbl.__SrtOrderTest = srtCounter
                              	tbl.change_end(.t.)
                              		vBidsheetNoPrev = vBidSheetNoCur
                              	tbl.fetch_next()
                              end while
                              tbl.close()
                              end

                              Comment

                              Working...
                              X