Question

I have a table with 4 digit cost centers and rates and I need to match it with a table that has 4 digit cost centers. The problem is that the table with rates lists the cost centers in a different way. It uses "X" to mean "include all of these." So if it wants every cost center starting with 1 to have a certain rate it shows 1XXX has a certain rate. My cost center table has actual 4 digit numbers like 1000.

If every cost center in the rate table was at a 1 digit level I could handle this, but some cost centers have 3 digits and an X (100X) and some have just one digit and an X (1XXX). How can I handle all of the possible variations and make sure I match at the lowest level?

Rate table:
1xxx = $10
12xx = $5
123x = $2
2xxx = $15
3456 = $5
3xxx = $10

How do I make sure that cost center 1234 pulls in $2 while cost center 1000 pulls $10?

Was it helpful?

Solution

I can do it with 3 simple queries - maybe someone else can show us both how to combine them into one.

First - find all the matches between centers (list of centers to look up) and cost_centers (the rate table) - I called this query "All matches"

SELECT Centers.Center, Rates.cost_center, Rates.rate, Max(InStr([cost_center] & "x","x")-1) AS n
FROM Centers, Rates
GROUP BY Centers.Center, Rates.cost_center, Rates.rate, Left([center],InStr([cost_center] & "x","x")-1)=Left([cost_center],InStr([cost_center] & "x","x")-1)
HAVING (((Left([center],InStr([cost_center] & "x","x")-1)=Left([cost_center],InStr([cost_center] & "x","x")-1))=True));

Second, find the lowest level match, or the one with the highest "n" in the prior query - I called this "selected matches"

SELECT [All matches].Center, Max([All matches].n) AS n
FROM [All matches]
GROUP BY [All matches].Center;

Finally, get the corresponding rate:

SELECT [All matches].Center, [All matches].cost_center, [All matches].rate
FROM [All matches] INNER JOIN [selected matches] ON ([All matches].n = [selected matches].n) AND ([All matches].Center = [selected matches].Center);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top