0

我有 3 个查询,它们根据条件检索最大值。

select max(to_number(substr(attr_value,9)))+1 
  from circ_inst inner
  join circ_attr_settings 
    on circ_inst.circ_inst_id=circ_attr_settings.circ_inst_id 
   and val_attr_inst_id=1045 
 where circ_attr_settings.attr_value like 'IPANEMA-%' 



select max(to_number(substr(attr_value,10)))+1 
  from circ_inst inner
  join circ_attr_settings 
    on circ_inst.circ_inst_id=circ_attr_settings.circ_inst_id 
   and val_attr_inst_id=1045 
 where circ_attr_settings.attr_value like 'FIREWALL-%'



select max(to_number(substr(attr_value,16)))+1 
  from circ_inst 
 inner join circ_attr_settings
    on circ_inst.circ_inst_id=circ_attr_settings.circ_inst_id
   and val_attr_inst_id=1045 
 where circ_attr_settings.attr_value like 'LAYER2 SWITCH-%'

我想从这 3 个查询中获得最大数量(如果 3 个查询的结果分别为 6430、6434 和 6418,那么我想获得值“6434”,这是三个结果中的最大值。

我尝试使用max(query1,query2,query3)但不成功。

4

2 回答 2

5

我认为您可以通过一个查询来做到这一点:

select max(to_number(substr(attr_value,instr(attr_value, '-')+1)))+1
from circ_inst inner join
     circ_attr_settings
     on circ_inst.circ_inst_id=circ_attr_settings.circ_inst_id and
        val_attr_inst_id=1045
where circ_attr_settings.attr_value like 'LAYER 2 SWITCH-%') or
      circ_attr_settings.attr_value like 'IPANEMA-%' or
      circ_attr_settings.attr_value like 'FIREWALL-%'

您的联接似乎都是相同的。唯一的区别是提取数字。您可以通过在值中查找“-”来获得正确的起始位置。

于 2012-07-19T13:23:47.813 回答
2

你也许可以这样做:

select greatest (
(select max(to_number(substr(attr_value,9)))+1 from circ_inst inner join circ_attr_settings on circ_inst.circ_inst_id=circ_attr_settings.circ_inst_id and val_attr_inst_id=1045 where circ_attr_settings.attr_value like 'IPANEMA-%'),
(select max(to_number(substr(attr_value,10)))+1 from circ_inst inner join circ_attr_settings on circ_inst.circ_inst_id=circ_attr_settings.circ_inst_id and val_attr_inst_id=1045 where circ_attr_settings.attr_value like 'FIREWALL-%'),
(select max(to_number(substr(attr_value,16)))+1 from circ_inst inner join circ_attr_settings on circ_inst.circ_inst_id=circ_attr_settings.circ_inst_id and val_attr_inst_id=1045 where circ_attr_settings.attr_value like 'LAYER 2 SWITCH-%')
) from dual;
于 2012-07-19T13:23:56.020 回答