Query for calculated field? (1 Viewer)

Roberto_Giussani

New member
Local time
Today, 21:48
Joined
Mar 28, 2023
Messages
9
I need to urgently resolve a problem.

I have a database with these 3 tables CUSTOMER - LESSONS RESERVATION BOOKING - PAYMENT.

I need the residual credit to be calculated in the AMOUNT OUTSTANDING field of the CUSTOMER table,

on the basis of the lessons booked or performed and on the basis of the payments made.

THANK YOU !
 

Attachments

  • CUSTOMER.jpg
    CUSTOMER.jpg
    105.9 KB · Views: 49
  • LESSONS.jpg
    LESSONS.jpg
    94.1 KB · Views: 52
  • PAYMENT.jpg
    PAYMENT.jpg
    71.2 KB · Views: 49
  • QUERY.jpg
    QUERY.jpg
    209.8 KB · Views: 48
  • QUESITO.pdf
    513.9 KB · Views: 59

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:48
Joined
May 21, 2018
Messages
8,529
Get rid of the amount outstanding in your table. Avoid calculated fields. In the query you show change it to a Left outer join so you get all custumers, all customer lessons, and all lesson payments.
Then do a "Group By" query. Group on CustomerID, and take sum of Lesson Price, and sum of payment. Save that query. Now join that to the customer table in a second query a and make a calculated column (AmountDue: [SumofLesson_Price] - [SumPayment_Amount])

If not familiar here is a "group by" example
 

Roberto_Giussani

New member
Local time
Today, 21:48
Joined
Mar 28, 2023
Messages
9
Get rid of the amount outstanding in your table. Avoid calculated fields. In the query you show change it to a Left outer join so you get all custumers, all customer lessons, and all lesson payments.
Then do a "Group By" query. Group on CustomerID, and take sum of Lesson Price, and sum of payment. Save that query. Now join that to the customer table in a second query a and make a calculated column (AmountDue: [SumofLesson_Price] - [SumPayment_Amount])

If not familiar here is a "group by" example
thank you for your reply ... but may be I make a mistake ... Query2 don't work
 

Attachments

  • Schermata 28-03-2023 22.54.31.jpg
    Schermata 28-03-2023 22.54.31.jpg
    137.5 KB · Views: 55
  • Schermata 28-03-2023 22.54.56.jpg
    Schermata 28-03-2023 22.54.56.jpg
    154.9 KB · Views: 60

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:48
Joined
May 21, 2018
Messages
8,529
It looks to me like like you did a Group By on the three fields instead of summing the other two fields
CustomerID: Group BY
LessonPrice: SUM
Payment_Amount: SUM

GroupBY.png
 

Roberto_Giussani

New member
Local time
Today, 21:48
Joined
Mar 28, 2023
Messages
9
It looks to me like like you did a Group By on the three fields instead of summing the other two fields
CustomerID: Group BY
LessonPrice: SUM
Payment_Amount: SUM

View attachment 107228

ok the first query work correctly ... but second one no ...

Now join that to the customer table in a second query a and make a calculated column (AmountDue: [SumofLesson_Price] -[SumPayment_Amount])

Sorry but I am principiant ... thank you from Italy
 

Attachments

  • Schermata 28-03-2023 23.26.05.jpg
    Schermata 28-03-2023 23.26.05.jpg
    97.1 KB · Views: 54

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:48
Joined
May 21, 2018
Messages
8,529
In the first query you did your group by and you should have got something like this with a row for each customer

Customer_IDSumOfLesson_PriceSumOfPayment_Amount
110050
2200200
3300

Use the above in a regular query linking customer_ID to table Customer. That is just a regular query not another aggregate query. You show a second aggregate.
 

Roberto_Giussani

New member
Local time
Today, 21:48
Joined
Mar 28, 2023
Messages
9
Nella prima query hai fatto il tuo gruppo e avresti dovuto ottenere qualcosa del genere con una riga per ogni cliente

Identificativo del clienteSumOfLesson_PriceSommaPagamento_Importo
110050
2200200
3300

Utilizzare quanto sopra in una normale query che collega customer_ID alla tabella Customer. Questa è solo una query normale, non un'altra query aggregata. Mostri un secondo aggregato.
Now work ... but how I can find or copy the "Amount outstanding" in the field of the customer table ?
 

Attachments

  • Schermata 29-03-2023 00.18.01.jpg
    Schermata 29-03-2023 00.18.01.jpg
    140.2 KB · Views: 44

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:48
Joined
May 21, 2018
Messages
8,529
In this query add a calculated field.
SumOutstanding: [SumOfLesson_Price] - [SommaPagamento_Importo]
 

Roberto_Giussani

New member
Local time
Today, 21:48
Joined
Mar 28, 2023
Messages
9
ok work correctly but now I want put the "Amount outstanding" in the field of the customer table ... how I can do ? I think that this is my last request
 

Attachments

  • Schermata 29-03-2023 00.32.48.jpg
    Schermata 29-03-2023 00.32.48.jpg
    143.1 KB · Views: 40
  • Schermata 29-03-2023 00.35.59.jpg
    Schermata 29-03-2023 00.35.59.jpg
    91.1 KB · Views: 48

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:48
Joined
May 21, 2018
Messages
8,529
You should not do that. If you put a calculated field in a table, then it is only correct at that point of time. Every time you edit the database records you would have to ensure to do an update query and push this calculated value into the table. Why do you need it in the table if you can show this in a report, form, or query dynamically?
If you had to do it you would now do an update query based on this final query.
 

Roberto_Giussani

New member
Local time
Today, 21:48
Joined
Mar 28, 2023
Messages
9
Non dovresti farlo. Se inserisci un campo calcolato in una tabella, allora è corretto solo in quel momento. Ogni volta che modifichi i record del database, dovresti assicurarti di eseguire una query di aggiornamento e inserire questo valore calcolato nella tabella. Perché ne hai bisogno nella tabella se puoi mostrarlo dinamicamente in un report, modulo o query?
Se dovessi farlo, ora faresti una query di aggiornamento basata su questa query finale.
osservazione corretta...grazie...creerò un pulsante nella scheda cliente che mi darà il valore aggiornato...grazie mille
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:48
Joined
May 21, 2018
Messages
8,529
Saving information that can be quickly calculated is always a bad idea. It is very easy to get the stored data out of synch with the "real" current data. Sometimes it if very long and resource intensive do do calculations. In that case prior to reporting you may want to push values into a temp table, but those values are only current at the point you run the report.
 

Roberto_Giussani

New member
Local time
Today, 21:48
Joined
Mar 28, 2023
Messages
9
one last clarification ... if I put a button in the customer form that executes the query, what should I enter to get the amount of the customer concerned as a result?
 

June7

AWF VIP
Local time
Today, 11:48
Joined
Mar 9, 2014
Messages
5,473
Cannot perform an UPDATE action SQL when an aggregate query is involved.

A SELECT query is not 'executed' - you pull data from it same as pulling from a table. Bind form or report to query or use domain aggregate function (DLookup, DCount, DSum, etc) to pull specific data into textbox.
 

Roberto_Giussani

New member
Local time
Today, 21:48
Joined
Mar 28, 2023
Messages
9
Cannot perform an UPDATE action SQL when an aggregate query is involved.

A SELECT query is not 'executed' - you pull data from it same as pulling from a table. Bind form or report to query or use domain aggregate function (DLookup, DCount, DSum, etc) to pull specific data into textbox.
Thank you for your reply. Sorry but I am principiant and don't understand what I have to do. Please con you send me a sample ? THANKS again
 

June7

AWF VIP
Local time
Today, 11:48
Joined
Mar 9, 2014
Messages
5,473
What do you not understand about suggested options? How to bind form/report to query or how to use domain aggregate function?

If you want example specific to your data and situation, provide your db for analysis.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:48
Joined
May 21, 2018
Messages
8,529
I kind of screwed up my previous suggestion. If you do it that way and you have multiple payments per lesson then you are not going to get the correct answer. See if this video is helpful. Also select the watch on youtube if not the first 10 or so seconds are blurry.

 

Attachments

  • Balance.accdb
    1.5 MB · Views: 75

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:48
Joined
May 21, 2018
Messages
8,529
FYI, the final query is not updateable so if you want to edit your customer data and include this you cannot. You could make this a subform on your customer form and link by customer id. Then you could show this data linked to the first customer in the footer of the form.

qryBalance qryBalance

CustomerIDLast NameFirst NameSumOfLessonPriceSumOfPayment_AmountBalance
1​
DavolioNancy
$180.00​
$150.00​
$30.00​
 

Users who are viewing this thread

Top Bottom