
Welcome to the nuBuilder Forums!
Register and log in to access exclusive forums and content available only to registered users.
Register and log in to access exclusive forums and content available only to registered users.
Calculate the sum in a form
-
- Posts: 27
- Joined: Thu Sep 01, 2022 3:42 am
- Has thanked: 1 time
Calculate the sum in a form
Hi, I have a table that has a field called 'item' and another field called 'quantity'; of course, the names of the articles may or may not be repeated. I'd like to know, how I can calculate the sum of these items with their respective amounts in the preview of the browser form and in the preview of the edit form. I've tried in many ways, but I still have not succeeded in doing it... 

-
- nuBuilder Team
- Posts: 4292
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 444 times
- Contact:
Re: Calculate the sum in a form
Hi,
To calculate the sum (or product ?) of the these columns, add a calculation to the Display column
As for the Edit form, add a calculated field (object of type Calc):
To calculate the sum (or product ?) of the these columns, add a calculation to the Display column
As for the Edit form, add a calculated field (object of type Calc):
You do not have the required permissions to view the files attached to this post.
-
- Posts: 27
- Joined: Thu Sep 01, 2022 3:42 am
- Has thanked: 1 time
Re: Calculate the sum in a form
Hi Kevin, nice to greet you again... Thanks for your previous explanation, the formula works very well for me when the expression is a multiplication between two fields with numerical values, but I still don't know how to add the records with the same names and I display the final result for each name with their respective values. I show you a snippet of what I want to do is:
You do not have the required permissions to view the files attached to this post.
-
- nuBuilder Team
- Posts: 4292
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 444 times
- Contact:
Re: Calculate the sum in a form
In a browse form, add this SQL:
Replace your_table_name with the actual name of your table in the database.
and replace your_table_name_id with the actual name of the primary key.
This query uses the SUM function to calculate the total quantity for each item, grouping the results by the item column.
Note that "NULL AS your_table_name _id" is required for the SQL parser to work.
Code: Select all
SELECT * FROM (
SELECT NULL AS your_table_name _id, item, SUM(quantity) as quantity FROM your_table_name
GROUP BY NULL, item
) T;
and replace your_table_name_id with the actual name of the primary key.
This query uses the SUM function to calculate the total quantity for each item, grouping the results by the item column.
Note that "NULL AS your_table_name _id" is required for the SQL parser to work.
-
- Posts: 27
- Joined: Thu Sep 01, 2022 3:42 am
- Has thanked: 1 time
Re: Calculate the sum in a form
Hi Kevin... I did everything you told me, following your instructions to the letter, but I still don't have good results. When the code you suggested is executed, the browse form goes completely blank. On the other hand, if I put part of the SQL code as I show you below, it only groups the names of the items, but it does not perform the corresponding sum of them; that is, it groups the items and only shows the first quantity, without adding the other common items.
I'm kind of baffled because I don't know what to do to make that work!!
I'm kind of baffled because I don't know what to do to make that work!!

Code: Select all
SELECT NULL AS stock_id, item, SUM(quantity) as quantity FROM stock GROUP BY NULL, item;
-
- nuBuilder Team
- Posts: 4292
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 444 times
- Contact:
Re: Calculate the sum in a form
Can you show the generated SQL? (Options Menu -> Form Info) and also the form properties where I can see your browse columns
-
- Posts: 27
- Joined: Thu Sep 01, 2022 3:42 am
- Has thanked: 1 time
Re: Calculate the sum in a form
Hi Kevin, here is the information you asked me for
You do not have the required permissions to view the files attached to this post.
-
- nuBuilder Team
- Posts: 4292
- Joined: Sun Oct 14, 2018 6:43 pm
- Has thanked: 71 times
- Been thanked: 444 times
- Contact:
Re: Calculate the sum in a form
The query you provided is almost correct. However, it is missing an alias for the subquery. Here's the corrected version:
Code: Select all
SELECT * FROM (
SELECT NULL AS stock_id, item, SUM(quantity) as quantity FROM stock
GROUP BY item
) T
-
- Posts: 27
- Joined: Thu Sep 01, 2022 3:42 am
- Has thanked: 1 time
Re: Calculate the sum in a form
Dude, you´re a genius!! The code works perfectly... thank you very much!!!