Hello Everyone,
I am having trouble creating a query that pulls aggregate data from separate tables.
Background:
My database tracks sales data. Each sales opportunity has an opportunity identifier (OppID). Each opportunity is related to a business unit (BU). Each opportunity has one (or more) vehicle models associated with it.
I track sales data on two tables. One table stores data at the opportunity level. The other stores data at a deeper level, the opportunity/vehicle level. Here are simplified versions of these tables:
Here is an example of the data my tables can contain:
Note: The sum of the PeakAnnRev values for the vehicles within an opportunity do NOT necessarily equal the PeakAnnRev value for the opportunity as a whole. In fact, they very often differ.
My task was to run a query that returns the number of opportunities and the sum of the PeakAnnRev of those opportunities, grouped by business Unit. I did this by running the following query:
When I run this query I get the following results:
However, my instructions were changed; now I want a query that returns the number of opportunities and the sum of the PeakAnnRev of the VEHICLES associated with those opportunities, grouped by business Unit. If I simply change my query to use the other table, like this:
I get the following results:
These are NOT the results I want. What I want is to get the NumOpps values from the first result set and the ValueOpps values from the second result set, like this:
I tried various ways of doing this with no success. I guess I am not experienced enough in SQL to figure it out.
So, does anyone have any ideas of how I can write a query that will get me the results as I've described them? Thank you very much for your help!
Thanks,
clakerley
I am having trouble creating a query that pulls aggregate data from separate tables.
Background:
My database tracks sales data. Each sales opportunity has an opportunity identifier (OppID). Each opportunity is related to a business unit (BU). Each opportunity has one (or more) vehicle models associated with it.
I track sales data on two tables. One table stores data at the opportunity level. The other stores data at a deeper level, the opportunity/vehicle level. Here are simplified versions of these tables:
Code:
Table: Sales_Opp
Fields:
OppID, : String
BU, : String
PeakAnnRev : Double
Table: Sales_Vehicle
Fields:
OppID, : String
Vehicle : String
BU, : String
PeakAnnRev : Double
Here is an example of the data my tables can contain:
Code:
Sales_Opp:
OppID BU PeakAnnRev
----- -- ----------
Opp1 BU1 1000
Opp2 BU2 2000
Opp3 BU2 3000
Sales_Vehicle:
OppID Vehicle BU PeakAnnRev
----- ------- -- ----------
Opp1 Veh1 BU1 500
Opp1 Veh2 BU1 600
Opp2 Veh3 BU2 2000
Opp3 Veh4 BU2 2200
Opp3 Veh5 BU2 900
Note: The sum of the PeakAnnRev values for the vehicles within an opportunity do NOT necessarily equal the PeakAnnRev value for the opportunity as a whole. In fact, they very often differ.
My task was to run a query that returns the number of opportunities and the sum of the PeakAnnRev of those opportunities, grouped by business Unit. I did this by running the following query:
Code:
SELECT BU, Sum(1) as NumOpps, Sum(PeakAnnRev) as ValueOpps
FROM Sales_Opp
GROUP BY BU
ORDER BY BU
When I run this query I get the following results:
Code:
BU NumOpps ValueOpps
-- ------- ---------
BU1 1 1000
BU2 2 5000
However, my instructions were changed; now I want a query that returns the number of opportunities and the sum of the PeakAnnRev of the VEHICLES associated with those opportunities, grouped by business Unit. If I simply change my query to use the other table, like this:
Code:
SELECT BU, Sum(1) as NumOpps, Sum(PeakAnnRev) as ValueOpps
FROM Sales_Vehicle
GROUP BY BU
ORDER BY BU
I get the following results:
Code:
BU NumOpps ValueOpps
-- ------- ---------
BU1 2 1100
BU2 3 5100
These are NOT the results I want. What I want is to get the NumOpps values from the first result set and the ValueOpps values from the second result set, like this:
Code:
BU NumOpps ValueOpps
-- ------- ---------
BU1 1 1100
BU2 2 5100
I tried various ways of doing this with no success. I guess I am not experienced enough in SQL to figure it out.
So, does anyone have any ideas of how I can write a query that will get me the results as I've described them? Thank you very much for your help!
Thanks,
clakerley