1

我试图在我的 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
)
4

2 回答 2

1

我相信以下内容在功能上应该是等效的。

这假定使用的值属于MONEY数据类型...如果不是,您可以将此处使用的最小/最大值调整为正在使用的数据类型的值。

-- First, set the min / max magic numbers for the MONEY data type.
DECLARE
    @MinMoney MONEY,
    @MaxMoney MONEY

SET @MinMoney = -922,337,203,685,477.5808
SET @MaxMoney = 922,337,203,685,477.5807


-- Then later in your query...
WHERE
(
    CASE 
        -- First, handle the "no parameters supplied scenario" by returning NULL,
        --  which will cause the following check to fail.
        WHEN @MIN_PRICE IS NULL AND @MAX_PRICE IS NULL THEN NULL

        -- Next, supply the values for the various statuses.
        WHEN LIST_STATUS = 'Sold' THEN SOLD_PRICE
        WHEN VALUE_RANGE_PRICING = 'No' THEN LIST_PRICE_H
        ELSE LOW_LIST_PRICE

    -- Lastly, check if the supplied value falls within the range.  
    -- If a value isn't supplied, convert it to either the min or max value.
    END BETWEEN 
        ISNULL(NULLIF(@MIN_PRICE, 0), @MinMoney) 
        AND ISNULL(NULLIF(@MAX_PRICE, 0), @MaxMoney)
)
于 2012-12-17T23:41:09.423 回答
1

您需要将布尔表达式转换为整数,1 表示真,0 表示假,然后将它们与 1 进行比较以再次获得布尔值。你可以这样说case when EXPRESSION then 1 else 0 end

像这样的东西:

where 
case 
   when situation1 then
             case when condition1  then 1 
              else 0 
              end
    when situation2 then 
          case when conditon2 then 1
              else 0 end
    else 0 end
 = 1

在您的示例中:

像这样:

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
                    case when SOLD_PRICE BETWEEN @MIN_PRICE AND @MAX_PRICE then 1 else 0 end
                ELSE --Active and Contingent
                    CASE 
                        WHEN VALUE_RANGE_PRICING = 'No' THEN
                            case when LIST_PRICE_H BETWEEN @MIN_PRICE AND @MAX_PRICE then 1 else 0 end
                        ELSE --Price Range
                        case when 
                            LOW_LIST_PRICE BETWEEN @MIN_PRICE AND @MAX_PRICE
                            OR
                            LIST_PRICE_H BETWEEN @MIN_PRICE AND @MAX_PRICE
                            then 1 else 0 end
                    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
                    case when SOLD_PRICE <= @MAX_PRICE then 1 else 0 end
                ELSE --Active and Contingent
                    CASE 
                        WHEN VALUE_RANGE_PRICING = 'No' THEN
                            case when SEARCH_PRICE <= @MAX_PRICE then 1 else 0 end
                        ELSE --Price Range
                            case when LOW_LIST_PRICE <= @MAX_PRICE then 1 else 0 end
                    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
                    case when SOLD_PRICE >= @MIN_PRICE then 1 else 0 end
                ELSE --Active and Contingent
                    CASE 
                        WHEN VALUE_RANGE_PRICING = 'No' Then 
                            case when SEARCH_PRICE >= @MIN_PRICE then 1 else 0 end
                        ELSE --Price Range
                            case when LIST_PRICE_H >= @MIN_PRICE then 1 else 0 end
                    END
            END
        --else {do nothing} as no min/max prices supplied
    END
    = 1
)

您将需要仔细阅读此内容是否有错别字......

另外,请注意,这不会使用任何索引。要使其使用索引,您可以将其分解为联合或使用临时表。

于 2012-12-17T23:52:07.870 回答