我正在尝试用SQL编写过程。我想在将所有日期条目匹配到传递的输入参数之后,从表中打印带有“最大分数”的记录。现在,该代码仅打印与指定日期相对应的所有记录。在这种情况下,我不确定如何使用MAX()。
/* =========================================================
Procedure to find the Highest Movie Rating on a particular date.
Given, an inpDate (input Date) it first looks up all Movie Reviews
of that particular date.
Then, it checks a StandardizedReviews Table (sTbl) and
finds the MAXIMUM adjusted_score with the
Highest Review Score (the variable adjusted_score)
========================================================= */
Use StandardizedReviews
Drop Procedure if exists HighestReview;
GO
CREATE PROCEDURE HighestReview @inpDate DATE AS (
SELECT sTbl.adjusted_score "Max Score"
FROM dbo.StandardizedReviews sTbl
WHERE CAST(sTbl.date as DATE) = CAST(@inpDate as DATE)
GROUP BY sTbl.date, sTbl.adjusted_score
)
GO
exec HighestReview '2020-03-05'
GO
现在,我的输出看起来像:
但是我希望它只显示分数为100的记录。谢谢。
您只需要用MAX()AS'Max Score'来包装Adjusted_score值,它只会选择最高得分。您还可以删除组中对sTbl.adjusted_score的引用,并且只能按日期排序:
CREATE PROCEDURE HighestReview @inpDate DATE AS (
SELECT MAX(sTbl.adjusted_score) AS 'Max Score'
FROM dbo.StandardizedReviews sTbl
WHERE CAST(sTbl.Date AS DATE) = CAST(@inpDate AS DATE)
GROUP BY sTbl.Date
)