Solved A complicated query, anyone can help? Thanks a lot! (1 Viewer)

Maplem

New member
Local time
Tomorrow, 06:52
Joined
Jun 27, 2021
Messages
8
A complicated query, anyone can help? Thanks a lot!

query.jpg
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:52
Joined
Oct 29, 2018
Messages
21,515
Hi. Have you tried using a Totals query?

Code:
SELECT Serial, Product, Sum(Amount) As Total
FROM TableName 
GROUP BY Serial, Product
 

Maplem

New member
Local time
Tomorrow, 06:52
Joined
Jun 27, 2021
Messages
8
Hi. Have you tried using a Totals query?

Code:
SELECT Serial, Product, Sum(Amount) As Total
FROM TableName
GROUP BY Serial, Product
Thanks theDBguy, it doesn't work. Because ProA and NewA have different serial and product name (but they do have some relation). they can not sum such easily.
 

plog

Banishment Pending
Local time
Today, 17:52
Joined
May 11, 2011
Messages
11,663
How's a person or computer to know what serial/product values are the "same"? Have a table to sort it out?
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 17:52
Joined
Feb 28, 2001
Messages
27,273
This is basically a translation problem. You have to translate the product name or number to another name or number. You have to decide which one is the preferred answer and then use a JOIN query to look up the alternate name. You can then do a GROUP BY to keep like products together.
 

Maplem

New member
Local time
Tomorrow, 06:52
Joined
Jun 27, 2021
Messages
8
This is basically a translation problem. You have to translate the product name or number to another name or number. You have to decide which one is the preferred answer and then use a JOIN query to look up the alternate name. You can then do a GROUP BY to keep like products together.
does it means another table to keep such information? if do so, i have to creat a new table, and insert new data manually. Any method to avoid manual work?
 

Maplem

New member
Local time
Tomorrow, 06:52
Joined
Jun 27, 2021
Messages
8
How's a person or computer to know what serial/product values are the "same"? Have a table to sort it out?
if right([serial],3) are same, and right([product],1) are same also, will be regarded as a same product.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:52
Joined
May 7, 2009
Messages
19,246
SELECT Serial,
Dlookup("Product","yourTable","Serial = '" & [Serial] & "' And Nz([Remark]) <> 'New'") As Prod ,
Sum([Amount]) As Total
FROM yourTable
GROUP BY Serial
 
Last edited:

Maplem

New member
Local time
Tomorrow, 06:52
Joined
Jun 27, 2021
Messages
8
SELECT Serial,
Dlookup("Product","yourTable","Serial = '" & [Serial] & "' And Nz([Remark]) <> 'New'") As Prod ,
Sum([Amount]) As Total
FROM yourTable
GROUP BY Serial
it doesn't work, the file attached. it would be nice if you can take a look.
 

Attachments

  • ADB.accdb
    864 KB · Views: 84

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:52
Joined
May 7, 2009
Messages
19,246
it will not work because record 4 and 5 have different serial number?
what is your formula to include them? last 3 digit must be the same?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:52
Joined
May 7, 2009
Messages
19,246
see Table Query
 

Attachments

  • ADB.accdb
    864 KB · Views: 89

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:52
Joined
May 7, 2009
Messages
19,246
you should probably use an easy approach of creating Serial number (for New).
say only the first character is replaced with "X" (or "N") and the rest of digit is same,

example:

900367 ProdA
X00367 New

or create a Master table, example (tblAlternative)

AltSerial---------Serial-------Product
900367----------900367-----ProdA
388367----------900367-----ProdA

you can then Join this table to your


select tblAlternative.Serial, tblAlternative.Product, Sum(
.[Amount]) From tblAlternative
Left Join
On tblAlternative.AltSerial =
.Serial
Group by tblAlternative.Serial, tblAlternative.Product;
 

Maplem

New member
Local time
Tomorrow, 06:52
Joined
Jun 27, 2021
Messages
8
you should probably use an easy approach of creating Serial number (for New).
say only the first character is replaced with "X" (or "N") and the rest of digit is same,

example:

900367 ProdA
X00367 New

or create a Master table, example (tblAlternative)

AltSerial---------Serial-------Product
900367----------900367-----ProdA
388367----------900367-----ProdA

you can then Join this table to your


select tblAlternative.Serial, tblAlternative.Product, Sum(
.[Amount]) From tblAlternative
Left Join
On tblAlternative.AltSerial =
.Serial
Group by tblAlternative.Serial, tblAlternative.Product;
yes, of course. Actually the serial and the product name are from other parties, I have no right to define them. That's why cause troubles.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 18:52
Joined
Feb 19, 2002
Messages
43,414
When you store data from other applications, it is just that - data. It is not a PK. At best you might search on it. The only purpose for keeping it is to communicate with the other application. THEREFORE, use your OWN product code to group by since obviously the serial numbers will be different and so will prevent grouping.
 
Last edited:

Users who are viewing this thread

Top Bottom