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

Loop to get total of quantity in repeating section

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

    Loop to get total of quantity in repeating section

    This is a list control
    I'm trying to get the total of the QTY field with the SKU field.
    So, my end result would ideally be:

    SKU: 300000, QTY 2
    SKU: 300001, QTY 1

    And then I'd update the SQL database accordingly.

    I haven't mastered the loops. Would this be best in xbasic or javascript?

    2024-04-11_10-29-01.png
    Attached Files

    #2
    are you against getting the total from sql?
    Gregg
    https://paiza.io is a great site to test and share sql code

    Comment


      #3
      Nope. Right now I do a SQL UPDATE, but it won't update the field twice if it's in the list twice. It will update the Bag and Snickers once, but I can't get it to decrease the amount for snickers 2.

      Comment


        #4
        Here's what i have now. I've changed a few names for security.

        Code:
        sql = <<%sql%
        UPDATE Inventory
        SET quantity = quantity - 1
        WHERE itemid IN(SELECT Items.itemid
        FROM Sales
        LEFT OUTER JOIN (Items
        LEFT OUTER JOIN Inventory
        ON Items.itemid = Inventory.itemid )
        ON Sales.SALEID = Items.SALEID AND Inventory.quantity >= 1
        WHERE Sales.SALEID = :SALEID);
        %sql%

        Comment


          #5
          Basically, the initial script takes that list control, writes it to an 'Items' table, then this SQL script compares it to the Inventory table, and updates the Quantity field.

          Comment


            #6
            Right now it's clear as mud to me.
            It's obvious your goal is to update the Inventory table.
            I'm not sure how you're using the Sales and Items tables.

            My guess is that you're putting sales for a specific time period into the sales table, and then you want to reduce the total (item.id) in inventory by the sum of (sales.item.id) for a specific time period.
            (looks like my time period is covered by your sales.saleid).

            If I got that part right, then I can work on what should be the easy part.
            The sql statement doesn't look right to me (could be my ignorance).

            One more thing that will help is the flavor of sql you use.

            Gregg
            https://paiza.io is a great site to test and share sql code

            Comment


              #7
              Yeah, ADHD brain tracks all over the place.

              Point of sale system.

              Using just SQL. Not MySQL.

              I have a Sales table, Items table and Inventory table.

              Sales table is the base sale.
              Items is the sale items that are sold from that Sale.
              Inventory is the store's inventory table showing product details and quantity in stock.

              The list is the list of items being sold. This list is saved to the Items table. Both have a separate field called "SALEID" that links the two together. It's not the primary key for either. It's a random string.

              The goal is to update the Inventory table with the quantity of the Inventory item being sold. Right now that script works fine, but only if they buy one of each item. If they buy 2 of one item, it only takes 1 of the Quantity field in the Inventory table.

              All this is done right away when the sale is completed. The inventory is updated right away.

              Comment


                #8
                Is this any better?

                Code:
                sql = <<%sql%
                UPDATE Inventory
                SET quantity = quantity - 1
                WHERE itemid IN (
                SELECT i.itemid
                FROM Items i
                JOIN Sales s ON i.SALEID = s.SALEID
                JOIN Inventory inv ON i.itemid = inv.itemid
                WHERE s.SALEID = :SALEID
                AND inv.quantity >= 1 );
                %sql%​
                ​​

                Comment


                  #9
                  It looks like the real problem is in the subquery.
                  I don't have the structure of your tables, so I am making some educated guesses based on the image below.
                  Sample data and table structure are the difference between guessing and testing my response.
                  I believe getting the query below to work as expected will make the update query work as expected.

                  Code:
                  select sum(coalesce( i.qty,0))
                  from items i
                  left join sales s on s.saleid = i.saleid
                  where i.sku = s.sku and s.saleid = :saleid
                  group by i.saleid,i.sku
                  image.png
                  Gregg
                  https://paiza.io is a great site to test and share sql code

                  Comment


                    #10
                    Both of these got me in the right direction and solved my problem. Plus, found a solution to another bug while I was there. Thanks much for the time!!!

                    Comment


                      #11
                      details… show the final code, and ideally explain the hidden bug.
                      Gregg
                      https://paiza.io is a great site to test and share sql code

                      Comment


                        #12
                        I realized I don't actually need the Sales table. The Items table had the SALEID In it in order to do the linking. And I know you guys use the table aliases, but I'm not used to those yet. I'll modify this now that I know it works.

                        Code:
                        sql = <<%sql%
                                UPDATE Inventory
                                SET quantity = quantity - (
                                SELECT sum(coalesce( Quantity,0))
                                        FROM Items
                                    WHERE Items.SALEID = :SALEID and Inventory.itemid = Items.itemid
                                    GROUP BY Items.saleid, Items.itemid)
                                WHERE itemid IN (
                                SELECT Items.itemid
                                    FROM Items
                                        JOIN Inventory ON Items.itemid = Inventory.itemid
                                WHERE Items.SALEID = :SALEID
                                AND Inventory.quantity >= 1 );
                                %sql%​
                        The hidden bug was unrelated to this. It was when the list of products, which is not saved to anything until the sale is complete, saves to the database table. The problem was very rarely the fields would duplicate but not duplicate in the sale total. The order of things is 1. Enter sale details with sale total, etc. 2. Enter items (save list control as a After Ajax Callback when creating the sale), enters the payments, then sets the inventory levels.

                        The issue was, if the internet connection was slow, the javascript SaveListEdits would timeout and would start again. At least that's what I think happened. I increased the timeout and the symptom went away.

                        I was able to replicate it repeatedly in Live Preview so it was nice to see a supposed fix.

                        Comment


                          #13
                          Glad to see ya got it figured out.
                          If something isn't quite right, check back.
                          I can work with sql, but I am far more fluent in mysql (ie: I really like to test my sql express code before passing it an answer).
                          As for the aliases, I use them to make the columns easier to pick out as I'm reading the code.
                          Gregg
                          https://paiza.io is a great site to test and share sql code

                          Comment

                          Working...
                          X