我现在有一张基表暂时叫做表A吧(表字段大概有 area_no,is_pps,is_iess,is_employ,pay_fee等等)的数据需要一些条件进行过滤。
过滤的条件是通过在网页上的条件配置生成的一张条件表(表B)从而对表A的数据进行过滤。表A 字段如下:customer_id area_no is_pps is_iess is_employ pay_fee123456 810 1 1 1 20
123447 810 0 1 0 0
123457 810 0 1 0 50
123458 811 0 1 0 100
123459 811 0 0 0 50
123467 812 0 1 0 30
123468 812 0 1 1 100
123469 812 0 0 0 100表B为:map_filed_name 意思为对应A表的字段名
map_filed_value意思为对应A表中相应字段的值
map_filed_type决定我们查询语句中使用何种查询方式 比如:01代表使用这个条件的时候要使用‘=’ ,02 代表使用 ‘in’,05代表写查询语句是我们需要使用‘>=’和‘<=’
map_filed_name map_filed_value map_filed_type
area_no 810 02
area_no 811 02
is_iess 1 01
is_pps 0 01
is_employ 0 01
pay_fee 10 05
pay_fee 200 05
最后的结果应该为
123457 810 0 1 0 50
123458 811 0 1 0 100
不知道高手们清楚我的问题没~~~~谢谢!!我写了点 但是有很多地方还没想通的~~~应该是用动态SQL吧 请高手们多多指教!!!谢谢
过滤的条件是通过在网页上的条件配置生成的一张条件表(表B)从而对表A的数据进行过滤。表A 字段如下:customer_id area_no is_pps is_iess is_employ pay_fee123456 810 1 1 1 20
123447 810 0 1 0 0
123457 810 0 1 0 50
123458 811 0 1 0 100
123459 811 0 0 0 50
123467 812 0 1 0 30
123468 812 0 1 1 100
123469 812 0 0 0 100表B为:map_filed_name 意思为对应A表的字段名
map_filed_value意思为对应A表中相应字段的值
map_filed_type决定我们查询语句中使用何种查询方式 比如:01代表使用这个条件的时候要使用‘=’ ,02 代表使用 ‘in’,05代表写查询语句是我们需要使用‘>=’和‘<=’
map_filed_name map_filed_value map_filed_type
area_no 810 02
area_no 811 02
is_iess 1 01
is_pps 0 01
is_employ 0 01
pay_fee 10 05
pay_fee 200 05
最后的结果应该为
123457 810 0 1 0 50
123458 811 0 1 0 100
不知道高手们清楚我的问题没~~~~谢谢!!我写了点 但是有很多地方还没想通的~~~应该是用动态SQL吧 请高手们多多指教!!!谢谢
from b where map_filed_type = '01'
union
select ' and '||map_filed_name||' in ('||wm_sys.wm_concat(map_filed_value)||' )'
from b where map_filed_type = '02' group by map_filed_name
union
select ' and '||map_filed_name||' >= '||min(map_filed_value)||' and '||map_filed_name||' <= '||max(map_filed_value)
from b where map_filed_type = '05' group by map_filed_name;
v_sql varchar2(4000) := ' select * from a where 1 = 1 ';
begin
for i in (select ' and '||map_filed_name||' = ' ||map_filed_value v
from b where map_filed_type = '01'
union
select ' and '||map_filed_name||' in ('||wm_sys.wm_concat(map_filed_value)||' )' v
from b where map_filed_type = '02' group by map_filed_name
union
select ' and '||map_filed_name||' >= '||min(map_filed_value)||' and '||map_filed_name||' <= '||max(map_filed_value) v
from b where map_filed_type = '05' group by map_filed_name) loop
v_sql := v_sql ||i.v;
end loop;
dbms_output.put_line(v_sql);
end;
/
是wmsys.wm_concat
一般不用加wmsys.也可以。10g以上的版本可以使用
shiyiwan这个过程写得真棒
wmsys我写错了,呵呵。
还有点点小问题 就是当map_filed_type=‘03’ map_filed_name =‘pay_fee’(A表中为NUMBER类型) 的时候和当map_filed_type=‘03’ map_filed_name =‘stat_date’(在A表中此字段存储为DATE类型,但是在B表中的时候存放的为条件类型为NUMBER) 的时候这样写SQL就有点问题了 涉及到引号的使用的问题 。 这里就涉及到了2种类型 而且我并不知道到底传过来的是何种类型。因为假如为number和date类型的话查询语句的写法就不一样了 一个不需要用 '' date需要用''。 所以我就考虑了应该要用函数来解决,但是 这样又会出现一个问题 我无法判断哪个大哪个小 就是不知道何时用 >= 何时用 <=