Hi,
I need to make an AVG() per category. The average price will select the top 200 most recent days (the format of the date is a txt format like "20090819" ) of each category. What I also need is that if the category does not have 200 days, then not to select the category (If there is a category of only 15 days, I don't need to have the average, but just to ignore that category)
Currently, I am capable of doing that, but only by using three queries one on top of each other... and it does take some time to run it too.
1. I would select the top 200 days of every category. This will return the top 200, even if there are many categories with less than 200 days.
2. I would do a count() and group by category to see what categories contain 200 (Using the 1st query above)
3. I would run the AVG() price on the 2nd Query.
I know there is probably a way to combine this, since it doesn't look that hard...
But I can't seem to do it in one single query...
The closest attempt I've got from an Access user is this code:
************************************************
SELECT YourTable.Cat, YourTable.DateField, Avg(YourTable.Price) AS AvgOfPrice
FROM YourTable
GROUP BY YourTable.Cat, YourTable.DateField
HAVING (((YourTable.Cat) In (SELECT YourTable.Cat FROM YourTable
GROUP BY YourTable.Cat HAVING (((Count(YourTable.Cat))>200))))
AND ((YourTable.DateField) In (SELECT top 200 DateField from YourTable )));
**************************************************************
It looks like it's working fine, but the only problem is that it returns 2 instances of every Category (named Cat here) and each one would get the 1st and 2nd DateField.
I know that Group by is supposed to return only ONE category, not two...
I need to make an AVG() per category. The average price will select the top 200 most recent days (the format of the date is a txt format like "20090819" ) of each category. What I also need is that if the category does not have 200 days, then not to select the category (If there is a category of only 15 days, I don't need to have the average, but just to ignore that category)
Currently, I am capable of doing that, but only by using three queries one on top of each other... and it does take some time to run it too.
1. I would select the top 200 days of every category. This will return the top 200, even if there are many categories with less than 200 days.
2. I would do a count() and group by category to see what categories contain 200 (Using the 1st query above)
3. I would run the AVG() price on the 2nd Query.
I know there is probably a way to combine this, since it doesn't look that hard...
But I can't seem to do it in one single query...
The closest attempt I've got from an Access user is this code:
************************************************
SELECT YourTable.Cat, YourTable.DateField, Avg(YourTable.Price) AS AvgOfPrice
FROM YourTable
GROUP BY YourTable.Cat, YourTable.DateField
HAVING (((YourTable.Cat) In (SELECT YourTable.Cat FROM YourTable
GROUP BY YourTable.Cat HAVING (((Count(YourTable.Cat))>200))))
AND ((YourTable.DateField) In (SELECT top 200 DateField from YourTable )));
**************************************************************
It looks like it's working fine, but the only problem is that it returns 2 instances of every Category (named Cat here) and each one would get the 1st and 2nd DateField.
I know that Group by is supposed to return only ONE category, not two...