Reworking the question to hopefully avoid any further reduction in reputation -- sorry for being new!
I am working on a relational MS-Access database that I created at work. I am self-taught, so I thoroughly appreciate the help!
The issue I am currently facing is that I need to set each independent transaction (RefID) with 1 customer name. Unfortunately, the systems we use will allow for multiple Customer Names to exist on one RefID. For example (an excerpt from the AR Data Table that I am using):
Ref ID Customer Name AR
20164444 Liberty 50
20164444 Liberty 75
20164444 Unicorn 100
20164444 Liberty 2500
As you can see, there are multiple Customer Names on one RefID. I have been spinning my wheels trying to use a sum on the AR Field and retrieving the Max value. Unfortunately, each time I try to pull it into a new table, both the Customer Names come over. An example of the current code being used:
SELECT AR.Refid, max(AR.collectamount) as MCA
FROM AR
GROUP by AR.RefID
This code is currently pulling all of the RefIds and the MCA or Max Collection Amount. However, if I try to bring in Customer Name from my mapping table:
SELECT AR.Refid, Max(AR.collectamount) AS MCA, [Customer Mapping Table].[Parent Name]
FROM AR INNER JOIN [Customer Mapping Table] ON AR.chkrcvdfrom = [Customer Mapping Table].[Child Name - IE]
GROUP BY AR.Refid, [Customer Mapping Table].[Parent Name];
Unfortunately, this code brings my records up to 147,977 and I have established that I should only have 145,395 unique records. This suggests that duplicates exist on RefID's. Please see the following example of a transaction on which I have found a failure:
Ref ID Customer Name AR
20161100749 TBC 2517
20161100749 Unicorn 30
At the end of the day, I need all 145,395 RefID's to have only one Customer Name. If there are two customers, I need the Customer Name with the most AR activity. All of the data that is required to make that possible sits within the AR Data File -- a data dump of our AR activity on a per RefID basis. I have an additional table called "Customer Mapping Table", where I have created Parent-Child relationships with some of our customers. For any solution to work I will need to Query the AR Table, pull the max AR Value of each customer, and isolate each RefID to only one Parent Name from the mapping table.
Edit: An attempt at a Minimal, Reproducible Example
The main source of my data is the AR Table -- for the purposes of this exercise, the fields are as follows:
Ref ID Customer Name CollectAmount itemdesc
20161100749 TBC 2517 Ocean_Freight
20161100749 Unicorn 30 Insurance_Premium
20161100749 TBC 30 AMS Fee
20161100749 TBC 100 Pre-Pull
20161100749 TBC 225 Trucker Yard Storage
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 125 Handling Charge
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
The section above can be copy and pasted as a table within MS-Access and called "AR" -- this will mirror the MS-Access Environment I am working within.
I took Lee's code and implemented it as follows:
SELECT t1.*
FROM AR AS t1 INNER JOIN
(select t.refid, max(t.collectamount) as MCA
GROUP by AR.RefID)
As t2 ON (t1.collectamount = t2.mca) AND (t1.refid = t2.refid);
When I hit "Run" no error code appears, but the query returns the following:
Ref ID Customer Name CollectAmount itemdesc
20161100749 TBC 2517 Ocean_Freight
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
50180900002 Westlake 170 LOCAL_CARTAGE_DELIVERY
The end goal for the data would be that each of the two RefID's I provided will be presented in two, unique rows, with only the max AR, Customer Name, and itemdesc fields. I fear this is my fault and Philipxy was correct that I had not provided enough information. I hope this helps clarify the question.