S
Smacs
I have two tables that I need to pull revenue data from and link to a
customer, but am running into a problem.
Here's what the tables look like...
TABLE_1
CID
AcctNum
ProdID
Revenue
TABLE_2
AcctNum
ProdID
Revenue
Note: each table references different ProdIDs.
So the problem I'm having is that there are multiple AcctNums per CID (not
surprising - I'd just group by CID, if that were the only thing), but ALSO
some AcctNums map to multiple CIDs. I've determined that in these cases, the
CIDs that map to the same AcctNum ARE in fact the same customer, there have
just been multiple CIDs setup for them for some reason.
Now, I need to map all the revenue to a single CID, so my sense is that I
need to:
1. Flatten all the CIDs that map to a single AcctNum while summing together
all their revenue so that none of it is lost
2. Map the revenue from the Acctnum (as well as any OTHER AcctNums that map
to any of the CIDs that have been flattened) to the single CID, without
losing OR double counting any of the revenue from TABLE_2
What I had been trying to do was something like this, but of course it's
resulting in Revenue from TABLE_2 being counted again for every AcctNum that
maps to the same CID...
SELECT TABLE_1.CID, TABLE_2.ProdID, Sum(TABLE_2.Revenue) AS Revenue
FROM TABLE_1 INNER JOIN TABLE_2 ON TABLE_1.AcctNum=TABLE_2.AcctNum
GROUP BY TABLE_1.CID, TABLE_2.ProdID;
Would really appreciate some guidance here. Thank you in advance.
customer, but am running into a problem.
Here's what the tables look like...
TABLE_1
CID
AcctNum
ProdID
Revenue
TABLE_2
AcctNum
ProdID
Revenue
Note: each table references different ProdIDs.
So the problem I'm having is that there are multiple AcctNums per CID (not
surprising - I'd just group by CID, if that were the only thing), but ALSO
some AcctNums map to multiple CIDs. I've determined that in these cases, the
CIDs that map to the same AcctNum ARE in fact the same customer, there have
just been multiple CIDs setup for them for some reason.
Now, I need to map all the revenue to a single CID, so my sense is that I
need to:
1. Flatten all the CIDs that map to a single AcctNum while summing together
all their revenue so that none of it is lost
2. Map the revenue from the Acctnum (as well as any OTHER AcctNums that map
to any of the CIDs that have been flattened) to the single CID, without
losing OR double counting any of the revenue from TABLE_2
What I had been trying to do was something like this, but of course it's
resulting in Revenue from TABLE_2 being counted again for every AcctNum that
maps to the same CID...
SELECT TABLE_1.CID, TABLE_2.ProdID, Sum(TABLE_2.Revenue) AS Revenue
FROM TABLE_1 INNER JOIN TABLE_2 ON TABLE_1.AcctNum=TABLE_2.AcctNum
GROUP BY TABLE_1.CID, TABLE_2.ProdID;
Would really appreciate some guidance here. Thank you in advance.