I am having a hard time developing a proper scalar function to work as intended. It needs to take an input of money type. I have a Products table, with a UnitPrice column that I am focused on. I need my function to sort products in descending order of unit prices, with the largest price having rank 1, next rank 2 etc. Same prices should rank the same and rank numbers must be sequentially and continuously assigned without skipping or jumping any numbers. That is, the ranks should look like 1, 2, 3, 4, 5,......, 61, and 62. Then, my function will find the rank of product(s) whose unit price equals to the input money value and return the rank number. If the input money value does not exist in the Products table, it returns -1. For example, because there are four products with a unit price of $18 and they are all ranked to be 40, RankOfGivenPrice(18) should return 40. Because no products have a unit price of $5.00, RankOfGivenPrice(5) returns -1.
My attempt:
go
create function RankOfGivenPrice
(@unitprice money)
returns money
begin
return
(select TOP 1
rank() over(order by UnitPrice desc) as RankNum
from Products
where UnitPrice = @unitprice)
end
go
This can compile, but it doesn't product the output I need. I call the function like select dbo.RankOfGivenPrice(18) but it returns 1.00. If I do not use TOP 1, I receive the "Subquery returned more than 1 value." error on function call. I assume I should be using the RANK() function but it is difficult with subquery.