Hello, I need help with this scalar-valued function.
What I want to do is to return the value I get in MaxValue on the line max(Value) AS MaxValue
.
The query works and will only return 1 value if ItemId
and ListPropertyId
exists, but I am not able to create a function of it.
CREATE FUNCTION GetLpivMax
(
-- Add the parameters for the function here
@ItemId int,
@ListPropertyId int
)
RETURNS int
AS
BEGIN
DECLARE @output INT;
WITH U AS (
SELECT i.Id AS ItemId,
lpiv.Value,
lp.Id AS ListPropertyId
FROM ListPropertyItemValues lpiv
JOIN ListPropertyItems lpi ON lpi lpi.Id = lpiv.ListPropertyItemId
JOIN ListProperties lp ON lp.Id = lpi.ListPropertyId
JOIN Items i ON i.Id = lpiv.ItemId)
SELECT @output = MAX(u.value)
FROM U u
WHERE u.listpropertyid = @ListPropertyId
AND u.itemid = @ItemId
GROUP BY u.listpropertyid, u.itemid
RETURN @output
END
GO