I have a table which has a logic with a specific priority for example:
Now based on client, employee and task, I need to find the correct Price using the Priority (this column is not part of the table, I just put it there to describe the logic)
I need to create a query that for example if I send 1, 1, 1, then return Price 100 (which is my first priority), if not found then I need to search with the second priority using 1, 1, null, if found then return Price 200 and so on until priority 5 which is the last one and the Price will be 120. Once I found the Price, I should return the value. I was thinking to use CTE but not sure how fast will be.
I was thinking this but I'm not sure about performance
declare @CompanyId INT = 7199
declare @EmployeeId INT = NULL
declare @TaskId INT = NULL
declare @DefaultPrice DECIMAL (9, 2) = 2
select coalesce(
(select Price from dbo.MyTable where CompanyId = @CompanyId AND EmployeeId = @EmployeeId AND TaskTypeId = @TaskId),
(select Price from dbo.MyTable where CompanyId = @CompanyId AND EmployeeId = @EmployeeId AND TaskTypeId IS NULL),
(select Price from dbo.MyTable where CompanyId = @CompanyId AND EmployeeId IS NULL AND TaskTypeId = @TaskId),
(select Price from dbo.MyTable where CompanyId = @CompanyId AND EmployeeId IS NULL AND TaskTypeId IS NULL),
(select Price from dbo.MyTable where CompanyId IS NULL AND EmployeeId IS NULL AND TaskTypeId IS NULL),
@DefaultPrice
)