I have an SQL table:
AR_Customer_ShipTo
+--------------+------------+-------------------+------------+ | ARDivisionNo | CustomerNo | CustomerName | ShipToCode | +--------------+------------+-------------------+------------+ | 00 | 1234567 | Test Customer | 1 | | 00 | 1234567 | Test Customer | 2 | | 00 | 1234567 | Test Customer | 3 | | 00 | ARACODE | ARACODE Customer | 1 | | 00 | ARACODE | ARACODE Customer | 2 | | 01 | CBE1EX | Normal Customer | 1 | | 02 | ZOCDOC | Normal Customer-2 | 1 | +--------------+------------+-------------------+------------+
(ARDivisionNo, CustomerNo,ShipToCode) form the primary key for this table.
If you notice that the first 3 lines belong to one client (Test Customer), which has different ShipToCodes: 1, 2 and 3. Similarly in the case of the second client (ARACODE client). Each normal client and regular client-2 has only 1 entry with one ShipToCode .
Now I would like to receive a query for the results in this table, where I will have only 1 record for each client. Thus, for any client with more than 1 record, I would like to save the record with the highest value for ShipToCode .
I tried different things:
(1) I can easily get a list of customers with one entry in the table.
(2) With the following query, I can get a list of all customers who have more than one record in the table.
[Request-1]
SELECT ARDivisionNo, CustomerNo FROM AR_Customer_ShipTo GROUP BY ARDivisionNo, CustomerNo HAVING COUNT(*) > 1;
(3) Now, to select the correct ShipToCode for each record returned by the above query, I cannot figure out how to iterate over all the records returned by the above query.
If I do something like:
[Request-2]
SELECT TOP 1 ARDivisionNo, CustomerNo, CustomerName, ShipToCode FROM AR_Customer_ShipTo WHERE ARDivisionNo = '00' and CustomerNo = '1234567' ORDER BY ShipToCode DESC
Then I can get the corresponding entry for (00-1234567-Test Customer). Therefore, if I can use all the results of query-1 in the above query (query-2), then I can get the desired individual records for clients with more than one record. This can be combined with the results from point (1) to achieve the desired end result.
Again, this may be easier than the approach I am following. Please let me know how I can do this.
[Note. I have to do this using only SQL queries. I cannot use stored procedures since I am going to accomplish this thing, finally using "Scribe Insight", which allows me to write queries.]