如何返回SQL Server存储过程的计算(聚合)结果

时间:2015-09-30 13:09:14

标签: sql sql-server stored-procedures sum aggregate

我正在尝试编写一个存储过程,它将返回一个数字(平均值) - 这就是我到目前为止所拥有的...

ALTER PROCEDURE [dbo].[sp_GetAverageRating]
    @RecipeNodeId int 
AS 
    SET NOCOUNT ON;

    (SELECT SUM(Rating) AS RatingTotal
     FROM dbo.RecipeRating
     WHERE RecipeNodeId = @RecipeNodeId)

    (SELECT 
         COUNT(Rating) AS RatingEntries 
     FROM dbo.RecipeRating 
     WHERE RecipeNodeId = @RecipeNodeId)

所以这有效并且给了我

A。)评级总计
B.)评级条目数

我想从存储过程中返回评级总计/评级条目。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

只需使用AVG功能:

SELECT AVG(Rating) as RatingTotal
FROM dbo.RecipeRating
WHERE RecipeNodeId = @RecipeNodeId