How can i sum values in Result 1 like Result 2
Result 1
SalesRep Customer Product Quantity Total
Doe, John AA Corp P1 50 5000
Doe, John CA Corp P2 67 6030
Doe, John EA Corp P3 46 5980
Doe, John GA Corp P2 22 1980
Doe, John HA Corp P3 43 5590
Doe, John IA Corp P1 35 3500 | Sum this two
Doe, John IA Corp P2 24 2160 | make second record 0
Doe, John JA Corp P2 66 5940
Doe, John MA Corp P4 59 7670
Doe, John OA Corp P2 43 3870 | Sum this two
Doe, John OA Corp P4 14 1820 | make second record 0
Doe, John PA Corp P1 89 8900
Result 2
SalesRep Customer Product Quantity TotalPrice
Doe, John AA Corp P1 50 5000
Doe, John CA Corp P2 67 6030
Doe, John EA Corp P3 46 5980
Doe, John GA Corp P2 22 1980
Doe, John HA Corp P3 43 5590
Doe, John IA Corp P1 59 5660
Doe, John IA Corp P2 0 0
Doe, John JA Corp P2 66 5940
Doe, John MA Corp P4 59 7670
Doe, John OA Corp P2 57 5690
Doe, John OA Corp P4 0 0
Doe, John PA Corp P1 89 8900
One possibility is this, although I really don’t see the business case. I guess it’s just a technical question:
For the two
CASEclauses, they read: “when there is a record for the same sales representative and customer with a lesser product (that’s my assumption?), then make the value zero. Otherwise, sum up all values grouped by sales representative and customer. This will work also with more than two (distinct!) products per sales rep. and customer.Note: If there can be several identical products per sales rep. and customer, this won’t work, and you’d have to compare some other values (e.g.
T1.ID < T.ID) in the two nested selectsNote also: this could be wrong, as I had to make some assumptions regarding your requirements.