2012-07-19 55 views
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),但沒有成功

回答

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-%' 

您的加入看起來都一樣。唯一的區別是提取數字。您可以通過在值中查找「 - 」來獲得正確的開始位置。

+0

謝謝你的答案......可能是我比較喜歡這個,因爲它並不需要三個查詢 – Techie 2012-07-19 13:29:15

+0

這是一個更好的解決方案,特別是如果選擇的值可能需要在將來改變。 – 2012-07-19 13:36:19

+0

那麼,這個解決方案確實要求文本中只有一個「 - 」,並且它將數字部分分開。雖然這似乎是該領域的標準。 – 2012-07-19 13:37:39

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; 
+0

感謝您的回覆 – Techie 2012-07-19 13:28:43

相關問題