I'd like my procedure to return the total of AcceptedProducts
by PlantName
.
Could someone help me to correct my procedure?
CREATE PROCEDURE spAcceptedByCountry @Year INT, @Month INT
AS
SELECT CountryCode,
CountryName,
REPLACE(SUBSTRING([PlantName], CHARINDEX('-', [PlantName]), LEN([PlantName])), '-', '') AS [PlantName],
SUM(AcceptedProducts) AS AcceptedProducts
FROM vAcceptedByCountry
WHERE YEAR(DateOfManufacture) = @Year AND MONTH(DateOfManufacture) = @Month
GROUP BY CountryCode,
CountryName,
PlantName,
AcceptedProducts
ORDER BY CountryCode;
GO
Thank you for your help. Kind Regards.
Try this:
CREATE PROCEDURE spAcceptedByCountry @Year INT, @Month INT
AS
SELECT CountryCode,
CountryName,
REPLACE(SUBSTRING([PlantName], CHARINDEX('-', [PlantName]), LEN([PlantName])), '-', '') AS [PlantName],
SUM(AcceptedProducts) AS AcceptedProducts
FROM vAcceptedByCountry
WHERE YEAR(DateOfManufacture) = @Year AND MONTH(DateOfManufacture) = @Month
GROUP BY 1, 2, 3
ORDER BY CountryCode;
GO
You don't want to group by the aggregate you are trying to calculate.
So, now you are calculating the accepted products per countrycode/countryname/plantname, where the plant name is not the column but the item in your select clause.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.