I have a table with the following sample data (the data and field names are fictionalized... I'm not trying to track pieces of fruit!):
I want to create a query which will generate the following:
Count is the count of FruitID for each unique Owner/Type/Location. Allocation is calculated as the Count divided by the total count for each unique Owner/Type entry. The trick here is that the Allocation is not calculated across each Owner (where the Allocation for John/Apple/Chicago would equal 1/6, because there are 6 records for John), but across each unique Owner/Type pair (so John/Apple/Chicago is calculated as 1/3 = 0.333, because there are 3 records for John/Apple).
I can do it without the Allocation field:
SELECT Owner, Type, Location, Count(FruitID) FROM MyTable GROUP BY Owner, Type, Location;
But I can't figure out how to add the Allocation field/calculation. Is there a single SQL statement that can get me the second table/recordset above? Thanks in advance!
Code:
FruitID Owner Location Type
------- ------ -------- ----
1 John New York Apple
2 John New York Apple
3 John New York Orange
4 John New York Banana
5 John New York Banana
6 John Chicago Apple
7 Mary Miami Banana
8 Mary Miami Banana
9 Mary Miami Banana
10 Mary Atlanta Banana
I want to create a query which will generate the following:
Code:
Owner Type Location Count Allocation
----- ---- -------- ----- ----------
John Apple New York 2 0.667
John Apple Chicago 1 0.333
John Banana New York 2 1.000
John Orange New York 1 1.000
Mary Banana Atlanta 1 0.250
Mary Banana Miami 3 0.750
Count is the count of FruitID for each unique Owner/Type/Location. Allocation is calculated as the Count divided by the total count for each unique Owner/Type entry. The trick here is that the Allocation is not calculated across each Owner (where the Allocation for John/Apple/Chicago would equal 1/6, because there are 6 records for John), but across each unique Owner/Type pair (so John/Apple/Chicago is calculated as 1/3 = 0.333, because there are 3 records for John/Apple).
I can do it without the Allocation field:
SELECT Owner, Type, Location, Count(FruitID) FROM MyTable GROUP BY Owner, Type, Location;
But I can't figure out how to add the Allocation field/calculation. Is there a single SQL statement that can get me the second table/recordset above? Thanks in advance!