0

我有一个查询,可以使用决策表根据多个标准做出决策。

假设在这里我需要知道一个结果(是或否),具体取决于一个人具有的属性,如表中所示。

DROP    TABLE IF EXISTS `tbl_decision`; 
CREATE TABLE `tbl_decision` (
  id_rule       INT NOT NULL primary key AUTO_INCREMENT,
  rule_name     VARCHAR(25) NOT NULL,
  minVal        INT NOT NULL,
  maxVal        INT NOT NULL,    
  decision      CHAR(1) NOT NULL,
  CONSTRAINT `uc_decision` UNIQUE (`id_rule`)           
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

Insert into `tbl_decision` VALUES
(1, 'WEIGHT'      , 60  ,100 ,'Y'),
(2, 'HEIGHT'      , 0   ,170 ,'Y'),
(3, 'Special rate', 10  ,30  ,'Y'),
(4, 'Consumption' , 0   ,3300,'N');

这是我要比较的表:

DROP    TABLE IF EXISTS `tbl_guy`; 
CREATE TABLE `tbl_guy` (
  id_guy    INT NOT NULL primary key AUTO_INCREMENT,
  guy_name     VARCHAR(25)  NOT NULL,
  weight        INT NOT NULL,
  height        INT NOT NULL,    
  rate          INT NOT NULL,    
  consumption   INT NOT NULL,    
  CONSTRAINT `uc_guy` UNIQUE (`id_guy`)           
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;


Insert into `tbl_guy` VALUES
(1, 'John'      , 200,220,10,1000),
(2, 'Mary'      , 100,160,11,5100),
(3, 'Diana'     , 80 ,155,19,4000),
(4, 'Ruthanne'  , 82 ,165, 5,2000);

该决定由以下查询确定。

SELECT *  FROM 
(
 SELECT id_guy,decision,
CASE WHEN rule_name = 'WEIGHT'       THEN tbl_guy.weight
     WHEN rule_name = 'HEIGHT'       THEN tbl_guy.height
     WHEN rule_name = 'Special rate' THEN tbl_guy.rate
     WHEN rule_name = 'Consumption'  THEN tbl_guy.consumption
END AS testQnty, rule_name, minVal, maxVal
FROM tbl_guy CROSS JOIN tbl_decision
) t 
WHERE minVal <= testQnty AND testQnty < maxVal 
ORDER BY id_guy;

我正在使用最小值和最大值来知道决定是否是,但是我不知道如何定义相反的规则。例如我定义:

(3, '特价', 10 ,30 ,'Y')

如果在场率上的家伙值在 10 到 30 之间,则为是

但是如果它在 0 和 3300 之间,则在字段消耗上决定是 NO

(4, '消费', 0 ,3300,'N');

我知道你总是可以使用补码来制定相反的规则,所以如果 0 <= x <= 3300 会变成 x < 0 AND x > 3300 但是如何在查询中启用它?

这是我的小提琴

其他

4

1 回答 1

2

您的变量decision似乎体现了两个概念。一个是规则的方向(是“介于”还是“不介于”),另一个是规则是否通过。为了澄清事情,以下将所有规则与一个新变量一起保留,以确定规则是否通过:

SELECT t.*,
       (case when decision = 'Y' and minVal <= testQnty AND testQnty < maxVal
             then 'Passed'
             when decision = 'N' and not (minVal <= testQnty AND testQnty < maxVal)
             then 'Passed'
             else 'Not Passed'
        end) as RuleResult
FROM (SELECT id_guy, decision,
             (CASE WHEN rule_name = 'WEIGHT'       THEN tbl_guy.weight
                   WHEN rule_name = 'HEIGHT'       THEN tbl_guy.height
                   WHEN rule_name = 'Special rate' THEN tbl_guy.rate
                   WHEN rule_name = 'Consumption'  THEN tbl_guy.consumption
              END) AS testQnty, rule_name, minVal, maxVal
      FROM tbl_guy CROSS JOIN tbl_decision
     ) t 
ORDER BY id_guy;

如果您只想要通过的规则:

SELECT t.*
FROM (SELECT id_guy, decision,
             (CASE WHEN rule_name = 'WEIGHT'       THEN tbl_guy.weight
                   WHEN rule_name = 'HEIGHT'       THEN tbl_guy.height
                   WHEN rule_name = 'Special rate' THEN tbl_guy.rate
                   WHEN rule_name = 'Consumption'  THEN tbl_guy.consumption
              END) AS testQnty, rule_name, minVal, maxVal
      FROM tbl_guy CROSS JOIN tbl_decision
     ) t
where (decision = 'Y' and minVal <= testQnty AND testQnty < maxVal) or
      (decision = 'N' and not (minVal <= testQnty AND testQnty < maxVal))
ORDER BY id_guy;
于 2013-07-13T18:12:37.830 回答