Obtaining 3 separate totals in one query (1 Viewer)

eduardo

Registered User.
Local time
Yesterday, 16:44
Joined
Jul 5, 2012
Messages
67
Hi there, I'm having a hard time figuring this out (I thought it was way simplier).

I have three tables A, B and C.

I want to build a query that displays three separate totals: Total of A, Total of B and Total of C.

When I try to do this, the query has a weird behaviour summing the totals of the totals of the totals, and I don't know why :banghead:

I'm attaching a really basic sample of what's happening, where totals SHOULD be $4, $8 and $16 instead of $16, $32 and $64.

Thanks!
 

Attachments

  • Database1.accdb
    456 KB · Views: 83

MarkK

bit cruncher
Local time
Yesterday, 16:44
Joined
Mar 17, 2004
Messages
8,180
It doesn't make sense to me to have one query do summaries on three different tables. What actual data is in the tables?
 

eduardo

Registered User.
Local time
Yesterday, 16:44
Joined
Jul 5, 2012
Messages
67
It doesn't make sense to me to have one query do summaries on three different tables. What actual data is in the tables?

Well it does to me, I need a form to display the three independent totals.
The tables contains values of three different type of loans. Their grand totals are unrelated.

So, anyone thinks this can be made?
 
Last edited:

MarkK

bit cruncher
Local time
Yesterday, 16:44
Joined
Mar 17, 2004
Messages
8,180
All your loans should be in the same table. Add a field called "LoanType" that stores your A, B, or C designations, and then you can do things like . . .
Code:
SELECT 
   Sum(IIF(LoanType="A", LoanValue, 0)) As TypeATotal,
   Sum(IIF(LoanType="B", LoanValue, 0)) As TypeBTotal
FROM tblLoan
Having the same structured data in different tables is a design error.
Hope this helps,
 

eduardo

Registered User.
Local time
Yesterday, 16:44
Joined
Jul 5, 2012
Messages
67
All your loans should be in the same table. Add a field called "LoanType" that stores your A, B, or C designations, and then you can do things like . . .
Code:
SELECT 
   Sum(IIF(LoanType="A", LoanValue, 0)) As TypeATotal,
   Sum(IIF(LoanType="B", LoanValue, 0)) As TypeBTotal
FROM tblLoan
Having the same structured data in different tables is a design error.
Hope this helps,

I appreciate your suggestion regarding the design, but the each type of loan needs separate autonumbering, physically, we have A-B-C folders stored in different rooms. So we can have a A-8540 folder and a B-8540 folder. (unrelated)

That is the reason why I can't have them in the same table. Not saying my design is perfect, but I don't believe it's that bad.

Anyway thank you
 

Frothingslosh

Premier Pale Stale Ale
Local time
Yesterday, 19:44
Joined
Oct 17, 2012
Messages
3,276
Autonumber fields should never contain real-world data. They are there to allow for primary keys and table linking, not data storage. If you need your folders numbered sequentially, it's trivial to have a form do that as new entries are made.

The reason you're having so much trouble is that your tables are not normalized - you have virtually identical data in multiple tables and are trying to relate them. If you were to combine them the way MarkK suggested, you would find working with the entire database to be much easier.

If you leave things as-is, you're just going to run into thornier and thornier problems as your database develops.
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 19:44
Joined
Jan 23, 2006
Messages
15,379
eduardo,

I agree with Mark. I believe you have a design issue. Further, I think you are misusing autonumbers. If you need a numbering scheme to match your room designations, then create fields for your use. Autonumbers are intended to be unique numbers only --NOT Incremental/sequential and not necessarily positive.
See this for more info on autonumbers.

Good luck with your project.
 

eduardo

Registered User.
Local time
Yesterday, 16:44
Joined
Jul 5, 2012
Messages
67
Autonumber fields should never contain real-world data. They are there to allow for primary keys and table linking, not data storage. If you need your folders numbered sequentially, it's trivial to have a form do that as new entries are made.

The reason you're having so much trouble is that your tables are not normalized - you have virtually identical data in multiple tables and are trying to relate them. If you were to combine them the way MarkK suggested, you would find working with the entire database to be much easier.

If you leave things as-is, you're just going to run into thornier and thornier problems as your database develops.


Thanks for the tip, I wish I could redo the whole db as you say, but at a first glance it will be a massive task. I'll think about it. Thanks!
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 19:44
Joined
Jan 23, 2006
Messages
15,379
They're always massive at first glance.
Do some research on Normalization. The sooner you do some tests with Normalized data, the sooner you'll see that task is not so large that it shouldn't be done. And you will learn a lot in the process.
 

vbaInet

AWF VIP
Local time
Today, 00:44
Joined
Jan 22, 2010
Messages
26,374
Thanks for the tip, I wish I could redo the whole db as you say, but at a first glance it will be a massive task. I'll think about it. Thanks!
Obviously I agree with everyone regarding the table structure but if you want to do what you're trying to do, is there any need of displaying all three in the same query if they're unrelated?

If all you want to do is display them side by side, create 3 separate totals queries, use the DLookup() function in 3 separate textboxes to get each of the values from the queries. Then put the textboxes side by side.

Otherwise, if you really need them side by side in a query then you can still create 3 totals queries, ensuring that they all return only 1 summed up value, then create another query and pull those three new queries into the new one.
 

eduardo

Registered User.
Local time
Yesterday, 16:44
Joined
Jul 5, 2012
Messages
67
Ok I've been testing a new possible structure of the db based on your advice (one table for all types of loans) where autonumber is not related to real-world data.

So if I were to add a new loan, for instance, loan type "H", shall I add the new loan number manually?

In this attached picture, the last loan "type H" was the number 8003 (West Co.)
If any user of my database wants to add a new "H" loan, it has to be the 8004 and if anyone adds a loan type "V" it has to be 1273.

I cannot trust users this task because it could lead to confusions/mistakes. So can this be automated? As far as I know there can only be a signle autonumber field in access?
 

Attachments

  • sample.JPG
    sample.JPG
    34 KB · Views: 181
Last edited:

MarkK

bit cruncher
Local time
Yesterday, 16:44
Joined
Mar 17, 2004
Messages
8,180
To get the Max + 1 for any LoanType you could write functions like . . .
Code:
Function GetNextLoanNumber(LoanType As String) As Long
[COLOR="Green"]'   Returns the next loan number for the given loan type[/COLOR]
    GetNextLoanNumber = GetMaxLoanNumber(LoanType) + 1
End Function

Function GetMaxLoanNumber(LoanType As String) as Long
[COLOR="Green"]'   Returns the max loan number for the given type, or zero[/COLOR]
    GetMaxLoanNumber = Nz(DMax("Number", "tblLoan", "LoanType = '" & LoanType & "'"), 0)
End Function
You can also add a unique index to the two fields LoanType and LoanNumber. Then the table will refuse any new records where the LoanType/LoanNumber combination being added already exists.
 

eduardo

Registered User.
Local time
Yesterday, 16:44
Joined
Jul 5, 2012
Messages
67
The DMAX shown in the sample is useful but not in my case, it can't generate different sequences of numbers based on the "type" that I explained above.

In regard to the 'cross post' complaint, does it infringe the forum terms of use or something?
I don't see what's the point of posting a link to whatever I've might posted out there.
 

eduardo

Registered User.
Local time
Yesterday, 16:44
Joined
Jul 5, 2012
Messages
67
To get the Max + 1 for any LoanType you could write functions like . . .
Code:
Function GetNextLoanNumber(LoanType As String) As Long
[COLOR=Green]'   Returns the next loan number for the given loan type[/COLOR]
    GetNextLoanNumber = GetMaxLoanNumber(LoanType) + 1
End Function

Function GetMaxLoanNumber(LoanType As String) as Long
[COLOR=Green]'   Returns the max loan number for the given type, or zero[/COLOR]
    GetMaxLoanNumber = Nz(DMax("Number", "tblLoan", "LoanType = '" & LoanType & "'"), 0)
End Function
You can also add a unique index to the two fields LoanType and LoanNumber. Then the table will refuse any new records where the LoanType/LoanNumber combination being added already exists.

Ops, I didn't get to read you before my last post.
I'll try to carry out your suggestion. Thank you.
 

jdraw

Super Moderator
Staff member
Local time
Yesterday, 19:44
Joined
Jan 23, 2006
Messages
15,379
The DMAx link I showed will work in your case if you find the Max sequence number for the given LoanType. It will take a little vba but nothing too complex.

As for cross posting see this link. It's more an etiquette thing than forums terms, but nobody wants to work in the dark at something if you have been getting help or have solved an issue elsewhere. A simple link to the other post(s) is sufficient. People here are volunteers who answer questions and assist in whatever way they can (often on many forums), but if you still don't see the point, then carry on.

If you need more help with vba, then a good reference is functionX
 

eduardo

Registered User.
Local time
Yesterday, 16:44
Joined
Jul 5, 2012
Messages
67
The DMAx link I showed will work in your case if you find the Max sequence number for the given LoanType. It will take a little vba but nothing too complex.

As for cross posting see this link. It's more an etiquette thing than forums terms, but nobody wants to work in the dark at something if you have been getting help or have solved an issue elsewhere. A simple link to the other post(s) is sufficient. People here are volunteers who answer questions and assist in whatever way they can (often on many forums), but if you still don't see the point, then carry on.

If you need more help with vba, then a good reference is functionX

Thanks for the guidance, I'm working on it, I'll post a sample once I get it done.

P.S.: I do see the point and agree, I'll remember it for future posts.
 

Users who are viewing this thread

Top Bottom