如果记录高于某个值,如何更新记录

时间:2017-09-11 13:58:23

标签: sql-server sql-update

当cv.From.Configtext小于或等于1200时,我使用以下代码将cvUpd中的可见更新为0.为此,我的查询检查cvFrom.ConfigText是否等于r。 value,驻留在OptionRestriction表中。

R 3.4.1.

下面,可以找到OptionRestriction表,可以看出,如果Feature_ID_1 - 1021中的OptionValue_1 - 1值低于1200,则OptionValue_2 - 4不应显示。

    UPDATE cvUpd
SET cvUpd.visible = case when cvFrom.ConfigText <= 1200 then 0 else 1 end
FROM ConfigValue cvUpd
INNER JOIN OptionRestriction r ON cvUpd.ConfigurationID=67151
   AND cvUpd.FeatureID = r.Feature_ID_2
   AND cvUpd.OptionValue= r.OptionValue_2
INNER JOIN ConfigValue cvFrom ON cvFrom.FeatureID =r.Feature_ID_1  
   AND cvFrom.ConfigurationID = cvUpd.ConfigurationID 
   AND cvFrom.OptionValue=r.OptionValue_1 
   AND cvFrom.ConfigText=CONVERT(nvarchar(max), r.value)
   AND (LEN(cvFrom.ConfigText) = 3 or LEN(cvFrom.ConfigText) = 4)

这一切都正常,因此正确的结果记录在ConfigValue表中(即当相应的OptionValue_1和Feature_ID_1的ConfigText为1200时,可见从1更新为0):

+--------------+---------------+-------+--------------+---------------+---------+
| Feature_ID_1 | OptionValue_1 | value | Feature_ID_2 | OptionValue_2 | visible |
+--------------+---------------+-------+--------------+---------------+---------+
|         1021 |             1 |  1200 |          775 |             4 |       0 |
+--------------+---------------+-------+--------------+---------------+---------+

但是,当Confixtext不是1200,而是1200以上的随机值时,我希望可见再次更改为1,因为现在可见保持0,当ConfigText更改为大于1200的值时(在下面的示例中为1300)。

+-----------------+-----------+-------------+----------+---------+-------+------------+
| ConfigurationID | FeatureID | OptionValue |   Name   | Visible | Value | ConfigText |
+-----------------+-----------+-------------+----------+---------+-------+------------+
|           67151 |       775 |           4 | OName    |       0 |     1 | 03-003     |
|           67151 |      1021 |           1 | Y-waarde |       1 |     0 | 1200       |
+-----------------+-----------+-------------+----------+---------+-------+------------+

我需要在查询中更改以获得以下结果(使用高于1200的ConfixText将可见设置为1)?

+-----------------+-----------+-------------+----------+---------+-------+------------+
| ConfigurationID | FeatureID | OptionValue |   Name   | Visible | Value | ConfigText |
+-----------------+-----------+-------------+----------+---------+-------+------------+
|           67151 |       775 |           4 | OName    |       0 |     1 | 03-003     |
|           67151 |      1021 |           1 | Y-waarde |       1 |     0 | 1300       |
+-----------------+-----------+-------------+----------+---------+-------+------------+

更新

这是创建OptionRestriction表的代码:

+-----------------+-----------+-------------+----------+---------+-------+------------+
| ConfigurationID | FeatureID | OptionValue |   Name   | Visible | Value | ConfigText |
+-----------------+-----------+-------------+----------+---------+-------+------------+
|           67151 |       775 |           4 | OName    |       1 |     1 | 03-003     |
|           67151 |      1021 |           1 | Y-waarde |       1 |     0 | 1300       |
+-----------------+-----------+-------------+----------+---------+-------+------------+

这是创建ConfigValue表的代码:

CREATE TABLE OptionRestriction(
   Feature_ID_1  INTEGER 
  ,Optionvalue_1 INTEGER 
  ,value         INTEGER
  ,Feature_ID_2  INTEGER 
  ,OptionValue_2 INTEGER 
  ,visible       INTEGER
);
INSERT INTO OptionRestriction(Feature_ID_1,Optionvalue_1,value,Feature_ID_2,OptionValue_2,visible) VALUES (1021,1,1200,775,4,0);

1 个答案:

答案 0 :(得分:1)

可能这就是你想要的:

UPDATE cvUpd
SET cvUpd.visible = case when cvFrom.ConfigText <= r.value then 0 else 1 end
FROM #ConfigValue cvUpd
INNER JOIN #OptionRestriction r ON cvUpd.ConfigurationID=67151
   AND cvUpd.FeatureID = r.Feature_ID_2
   AND cvUpd.OptionValue= r.OptionValue_2
INNER JOIN #ConfigValue cvFrom ON cvFrom.FeatureID =r.Feature_ID_1  
   AND cvFrom.ConfigurationID = cvUpd.ConfigurationID 
   AND cvFrom.OptionValue=r.OptionValue_1 
   AND (LEN(cvFrom.ConfigText) = 3 or LEN(cvFrom.ConfigText) = 4);