rubbernilly
Programmer
I have an odd situation. I have three tables that matter
WarrantyInformation
Vehicles
VehicleVolumes
A Vehicle entry can be used multiple times in the Warranty table, and multiple times in the Volumes table. The VehicleVolumes table has a primary key of Plant, ModelCode, and VolumeMonth, so that each entry is the Volume for that Plant, making that vehicle, this month.
What I need to be able to do is get a Total Volume from the VehicleVolumes table for any particular aggregation from the Warranty Table.
For instance, if I group on Vehicle, PartGroup, and TIS (time in service), I need the Vehicle Volume for the claims that match that. I cannot do a Sum(Volume), since that gives me multiples of the volume I should have. For example, I could have two records that match the grouping (Vehicle=1, PartGroup=87300, and TIS=6). If those two records are from the same Plant and ProdMonth (the other Primary Keys of the Volume table), then I only want the Volume once - they are both a part of the same production volume for the same plant for the same month for the same vehicle. A Sum(Volume) would give me twice the Volume I am actually looking for. Only if one of the records is from a different plant or different month of production would I then want a second month of Volume numbers, and then I would only want it once no matter how many records match that grouping.
I have tried solving this with a subquery to get the Sum(Volume) as an expression. I have a hundred queries, so I would prefer to solve this with a subquery rather than a pre-query. Here is my field declaration from the query I have been working with first:
And here is the SQL from the Query:
The problem is that this field declaration generates the following error:
[maroon]"You tried to execute a query that does not include the specified expression '' as part of an aggregate function."[/maroon]
If I change the field declaration of Total Volume to be this:
...then the query runs fine, except that this gets me the wrong Volume total.
So it seems that the problem is with the [W].[ProdMonth] field. The thing is, even if I add the [W].[ProdMonth] to the grouping of the main query, I get the same error.
What am I missing?
WarrantyInformation
Vehicles
VehicleVolumes
A Vehicle entry can be used multiple times in the Warranty table, and multiple times in the Volumes table. The VehicleVolumes table has a primary key of Plant, ModelCode, and VolumeMonth, so that each entry is the Volume for that Plant, making that vehicle, this month.
What I need to be able to do is get a Total Volume from the VehicleVolumes table for any particular aggregation from the Warranty Table.
For instance, if I group on Vehicle, PartGroup, and TIS (time in service), I need the Vehicle Volume for the claims that match that. I cannot do a Sum(Volume), since that gives me multiples of the volume I should have. For example, I could have two records that match the grouping (Vehicle=1, PartGroup=87300, and TIS=6). If those two records are from the same Plant and ProdMonth (the other Primary Keys of the Volume table), then I only want the Volume once - they are both a part of the same production volume for the same plant for the same month for the same vehicle. A Sum(Volume) would give me twice the Volume I am actually looking for. Only if one of the records is from a different plant or different month of production would I then want a second month of Volume numbers, and then I would only want it once no matter how many records match that grouping.
I have tried solving this with a subquery to get the Sum(Volume) as an expression. I have a hundred queries, so I would prefer to solve this with a subquery rather than a pre-query. Here is my field declaration from the query I have been working with first:
Code:
Total Volume: (SELECT Sum(V.Volume) AS SumOfVolume FROM VehicleVolumes AS V WHERE (((V.VehicleCode)=[W].[ModelCode]) AND ((V.VolumeMonth)=[W].[ProdMonth]) AND ((V.Plant)=[W].[Plant]));)
And here is the SQL from the Query:
Code:
SELECT W.VehicleName, W.ModelCode, W.PlantName, W.ModelYear, W.TIS, (SELECT Sum(V.Volume) AS SumOfVolume FROM VehicleVolumes AS V WHERE (((V.VehicleCode)=[W].[ModelCode]) AND ((V.VolumeMonth)=[W].[ProdMonth]) AND ((V.Plant)=[W].[Plant]));) AS [Total Volume], Sum(W.TotalWarrantyCost) AS [Total Cost], [Total Cost]/[Total Volume] AS CPU
FROM adm_WarrantyQuery_ProdMonth AS W
GROUP BY W.VehicleName, W.ModelCode, W.PlantName, W.ModelYear, W.TIS, W.Plant
HAVING (((W.TIS) Between [TIS Lower Limit] And [TIS Upper Limit]))
ORDER BY W.VehicleName, W.ModelCode, W.PlantName, W.ModelYear, W.TIS;
The problem is that this field declaration generates the following error:
[maroon]"You tried to execute a query that does not include the specified expression '' as part of an aggregate function."[/maroon]
If I change the field declaration of Total Volume to be this:
Code:
Total Volume: (SELECT Sum(V.Volume) AS SumOfVolume FROM VehicleVolumes AS V WHERE (((V.VehicleCode)=[W].[ModelCode]) AND ((V.VolumeMonth) [blue][b]Between [Start Date] AND [END DATE][/b][/blue]) AND ((V.Plant)=[W].[Plant]));)
...then the query runs fine, except that this gets me the wrong Volume total.
So it seems that the problem is with the [W].[ProdMonth] field. The thing is, even if I add the [W].[ProdMonth] to the grouping of the main query, I get the same error.
What am I missing?