逻辑表达式 - Oracle & C#

Logic expression - Oracle & C#

我在 Oracle 中有一个 table,其中包含大量数据。在结构中:

code_value    date_value     value
1             20/07/2017      10.5%
1             19/07/2017      11.6%
2             20/07/2017      1000.22%
2             18/07/2017      1700.44%

我还有一个table为这个数据定义了一个逻辑表达式:其结构如下:

code_value      check_rule    connection_rule
1                <16%              or
1                >30%              or
1                ==50%             or
2                >130%             and
2                <110%             and

我想在 table 上使用数据在 C# 上使用第二个 table 和 运行。
如何在 C# 上通过此 table 构建逻辑表达式?
喜欢:"if code_value <16% or code_value >30% or code_value ==50%".
可能吗?

谢谢!

如果您喜欢冒险,可以在单个 SQL 查询中完成,然后只需在 C# 应用程序中获取结果并显示它。

--CREATION OF THE TEST TABLES WITH DATA
create table test_data  ( data_id number, code_value number, date_value date, value varchar2(30) );
create table test_rules ( code_value number, check_rule varchar2(10), connection_rule varchar2(3));

insert into test_data values (1,1,to_date('20/07/2017','DD/MM/YYYY'),'10.5%');
insert into test_data values (2,1,to_date('19/07/2017','DD/MM/YYYY'),'11.6%');
insert into test_data values (3,2,to_date('20/07/2017','DD/MM/YYYY'),'1000.22%');
insert into test_data values (4,2,to_date('18/07/2017','DD/MM/YYYY'),'1700.44%');

insert into test_rules values (1,'<16%','or');
insert into test_rules values (1,'>30%','or');
insert into test_rules values (1,'==50%','or');
insert into test_rules values (2,'>130%','and');
insert into test_rules values (2,'<110%','and');

--THE QUERY
with 
connection_rules as ( select code_value, ' '||max(connection_rule)||' ' connection_rule  from test_rules group by code_value),
rules            as ( select code_value, substr(check_rule,0,1)||regexp_replace(check_rule,'[^0-9.]','')/100 as rule from test_rules),
checks           as ( select test_data.data_id, test_data.code_value, test_data.date_value, test_data.value, regexp_replace(test_data.value,'[^0-9.]','')/100 || ' ' || rules.rule as check1 from test_data join rules on test_data.code_value=rules.code_value),
expressions      as ( select checks.data_id, checks.date_value, checks.value, LISTAGG(checks.check1,connection_rules.connection_rule) WITHIN GROUP ( ORDER BY 1 ) as condition FROM checks JOIN connection_rules on checks.code_value =connection_rules.code_value group by checks.data_id, checks.code_value, connection_rules.connection_rule, checks.date_value, checks.value )
select expressions.data_id, expressions.date_value, expressions.value, substr(expressions.condition,0,50) condition, substr(xmlquery(expressions.condition returning content),0,10) as result  from expressions ;

结果:

DATA_ID DATE_VALUE  VALUE       CONDITION                           RESULT
1       20-JUL-17   10.5%       .105 <.16 or .105 =.5 or .105 >.3   true
2       19-JUL-17   11.6%       .116 <.16 or .116 =.5 or .116 >.3   true
3       20-JUL-17   1000.22%    10.0022 <1.1 and 10.0022 >1.3       false
4       18-JUL-17   1700.44%    17.0044 <1.1 and 17.0044 >1.3       false

只要您没有混合 AND 和 OR 条件的规则,它就可以工作。无论如何,你的数据结构不允许这样做,因为如果你想混合

,你必须有一个优先级的概念