我试图在我的 WHERE 子句中应用条件逻辑,使用带有“搜索的 CASE 表达式”的 CASE 语句,它返回一个表达式,但它不起作用,因为WHERE 子句需要一个“搜索条件”。见下文。
如何在 WHERE 子句中使用“搜索的 CASE 表达式”并让它返回“搜索条件”?
WHERE
(
--user supplies both min and max price
CASE
WHEN (@MIN_PRICE IS NOT NULL AND @MAX_PRICE IS NOT NULL AND @MIN_PRICE > 0 AND @MAX_PRICE > 0) THEN
CASE
WHEN LIST_STATUS = 'Sold' THEN
SOLD_PRICE BETWEEN @MIN_PRICE AND @MAX_PRICE
ELSE --Active and Contingent
CASE
WHEN VALUE_RANGE_PRICING = 'No' THEN
LIST_PRICE_H BETWEEN @MIN_PRICE AND @MAX_PRICE
ELSE --Price Range
LOW_LIST_PRICE BETWEEN @MIN_PRICE AND @MAX_PRICE
OR
LIST_PRICE_H BETWEEN @MIN_PRICE AND @MAX_PRICE
END
END
--user supplies just max price
WHEN ((@MIN_PRICE IS NULL OR @MIN_PRICE = 0) AND @MAX_PRICE IS NOT NULL AND @MAX_PRICE > 0) THEN
CASE
WHEN LIST_STATUS = 'Sold' THEN
SOLD_PRICE <= @MAX_PRICE
ELSE --Active and Contingent
CASE
WHEN VALUE_RANGE_PRICING = 'No' THEN
SEARCH_PRICE <= @MAX_PRICE
ELSE --Price Range
LOW_LIST_PRICE <= @MAX_PRICE
END
END
--user supplies just min price
WHEN ((@MAX_PRICE IS NULL OR @MAX_PRICE = 0) AND @MIN_PRICE IS NOT NULL AND @MIN_PRICE > 0) THEN
CASE
WHEN LIST_STATUS = 'Sold' THEN
SOLD_PRICE >= @MIN_PRICE
ELSE --Active and Contingent
CASE
WHEN VALUE_RANGE_PRICING = 'No'
SEARCH_PRICE >= @MIN_PRICE
ELSE --Price Range
LIST_PRICE_H >= @MIN_PRICE
END
END
--else {do nothing} as no min/max prices supplied
END
)