SQL where 句で。あればpv.SalePrice
、null
使いたいですpv.Price
。これどうやってするの?
WHERE
@FilterRangePriceValueMin < pv.SalePrice OR pv.SalePrice is null
AND (@FilterRangePriceValueMax > pv.SalePrice OR pv.SalePrice is null)
SQL where 句で。あればpv.SalePrice
、null
使いたいですpv.Price
。これどうやってするの?
WHERE
@FilterRangePriceValueMin < pv.SalePrice OR pv.SalePrice is null
AND (@FilterRangePriceValueMax > pv.SalePrice OR pv.SalePrice is null)
関数を使用してCOALESCE
アイテムを順番に試してからNULL
、比較のために最初の非 null アイテムを取得できます。BETWEEN
呼び出しを 2 回書き出すのを避けるために使用することもできます。
WHERE
COALESCE(pv.SalePrice, pv.Price)
BETWEEN @FilterRangePriceValueMin AND @FilterRangePriceValueMax
COALESCE
引数で最初の非 null 式を返す whichを使用できます。
WHERE
@FilterRangePriceValueMin < COALESCE(pv.SalePrice, pv.Price)
AND @FilterRangePriceValueMax > COALESCE(pv.SalePrice, pv.Price)
CASE
ステートメントを使用する
@FilterRangePriceValueMin < (CASE WHEN pv.SalePrice IS NULL THEN pv.Price ELSE pv.SalePrice END) OR pv.SalePrice is null
AND (@FilterRangePriceValueMax > (CASE WHEN pv.SalePrice IS NULL THEN pv.Price ELSE pv.SalePrice END) OR pv.SalePrice is null)
または、使用できますCOALESCE
引数の中で最初の非 null 式を返します。
価格は通常インデックス化されておらず、通常は適切なインデックス候補ではないため、これはストレッチである可能性があります。ただし、pv.SalePrice に適切な使用可能なインデックスがあり、pv.Price に別の適切な使用可能なインデックスがあり、テーブルが大きい場合、UNION
これに対する は COALESCE よりもはるかに高速に実行されます。
SELECT
...
FROM ...
WHERE pv.SalePrice>=@FilterRangePriceValueMin
AND pv.SalePrice<=@FilterRangePriceValueMax
UNION
SELECT
...
FROM ...
WHERE pv.Price>=@FilterRangePriceValueMin
AND pv.Price<=@FilterRangePriceValueMax
2 つのインデックス クエリは、完全なテーブル スキャンよりも高速になるという考えです。また、可能であれば使用するのが最善UNION ALL
ですが、(問題の限られた情報から)重複するかどうかはわかりません。
IFNULL
--------->を使用した別のアプローチMYSQL
WHERE
IFNULL(pv.SalePrice, pv.Price)
BETWEEN @FilterRangePriceValueMin AND @FilterRangePriceValueMax
NVL
--------------------------->ORACLE
WHERE
NVL(pv.SalePrice, pv.Price)
BETWEEN @FilterRangePriceValueMin AND @FilterRangePriceValueMax
ISNULL
--------------------------->SQL SERVER
WHERE
ISNULL(pv.SalePrice, pv.Price)
BETWEEN @FilterRangePriceValueMin AND @FilterRangePriceValueMax
私があなたを理解しているなら、私は試しませんが、あなたにとって役立つかもしれません.
(pv.SalePrice is not null and (@FilterRangePriceValueMin < pv.SalePrice AND @FilterRangePriceValueMax > pv.SalePrice ))
or
(pv.SalePrice is null and (@FilterRangePriceValueMin < pv.Price AND (@FilterRangePriceValueMax > pv.Price ) )
case 句を使用することをお勧めします。
CASE WHEN pv.SalePrice IS NULL THEN pv.SalePrice ELSE ' '