1

我在 SQL 服务器中有一个存储 MINIMUM_AMOUNT、MAXIMUM_AMOUNT 和 CURRENCY_ID 的表。现在我想构建一个 SQL 查询,它将检查要插入的新值是否已经存在于表中。例如:我的表有 2 条记录如下

RANGE_ID   MINIMUM_AMOUNT     MAXIMUM_AMOUNT    CURRENCY_ID
------------------------------------------------------------
 1         3000                 9000                3
 2         12000                17000               3

现在当用户插入一条新记录时,它不应该在已经可用的值之间

ie :用户不应该能够输入这些值对

  1 ) Min Amount : 4000  , Max Amount : 5000 ,Currency Id : 3
        because this range already lies in the first record (RANGE_ID 1)
  2)  Min Amount : 8000 , Max Amount : 10000,Currency d : 3
        because the minimum amount is already present in the range specified in first record (3000-9000)
  3)   Min Amount : 8000, Max Amount : 15000 , currency Id=3
       because the minimum amount is already present in one range and the maximum amount is also present in another range
  4)   Min Amount : 2500 , Max Amount : 11000 ,Currency Id=3
        because the this range overlaps with the data in first record

用户应该能够使用不同的货币 ID 输入上述范围。

我正在寻找一个 If Exists 查询来检查这一点。

4

3 回答 3

2

看看这个

DECLARE @Table TABLE(
        RANGE_ID INT,
        MINIMUM_AMOUNT FLOAT,
        MAXIMUM_AMOUNT FLOAT,
        CURRENCY_ID INT
)

INSERT INTO @Table (RANGE_ID,MINIMUM_AMOUNT,MAXIMUM_AMOUNT,CURRENCY_ID) SELECT 1,3000,9000,3
INSERT INTO @Table (RANGE_ID,MINIMUM_AMOUNT,MAXIMUM_AMOUNT,CURRENCY_ID) SELECT 2,12000,17000,3

DECLARE @NewMin FLOAT,
        @NewMax FLOAT,
        @CurrencyID INT

SELECT  @NewMin = 4000,
        @NewMax = 5000,
        @CurrencyID = 3

SELECT  *
FROM    @Table
WHERE   CURRENCY_ID = @CurrencyID
AND     NOT (MINIMUM_AMOUNT > @NewMax OR MAXIMUM_AMOUNT < @NewMin)  

这是检查的相反

  • 完全重叠
  • 部分重叠
  • 内部重叠
于 2009-12-11T06:36:02.790 回答
0

我认为这会做到:

IF EXISTS
(
    SELECT *
    FROM MinMaxTable mmt
    WHERE (mmt.CURRENCY_ID = @currencyID) AND
          (@minAmount BETWEEN mmt.MINIMUM_AMOUNT AND mmt.MAXIMUM_AMOUNT) AND
          (@maxAmount BETWEEN mmt.MINIMUM_AMOUNT AND mmt.MAXIMUM_AMOUNT)
)
BEGIN
    /* New data is within an existing range. */
END
ELSE
BEGIN
    /* Partially or completely outside an existing range. */
END;

用于IF NOT EXISTS反转条件。

于 2009-12-11T06:37:42.273 回答
0
select 1 
from currency_ranges R
where R.CURRENCY_ID = :newCurrency
and (
(R.MINIMUM_AMOUNT <= :newMin and R.MAXIMUM_AMOUNT >= :newMin)
or (R.MINIMUM_AMOUNT <= :newMax and R.MAXIMUM_AMOUNT >= :newMax)
)

这可以用作 EXISTS 语句来检查范围内的重叠

于 2009-12-11T07:44:20.497 回答