2012-07-19 66 views
-2

假設我有以下我應該使用哪個函數來解決

400001 - 400104 
400601 - 400614 
400701 - 400710 
401101 
401104 
401105 
401107 
401201 
401202 
401203 
401207 
401209 
401301 
401303 
421001 - 421005 
421201 
421202 
421203 
421204 
421301 
421304 
421306 

給出一個範圍,我必須找到以下的範圍不是告訴我做的最簡單的方法給出的數據。 我使用功能和解決,但如果有更多的範圍內它會變得更加繁瑣

IN 
      (400001, 400002, 400003, 400004, 400005, 400006, 400007, 400008, 
      400009, 400010, 400011, 400012, 400013, 400014, 400015, 400016, 
      400017, 400018, 400019, 400020, 400021, 400022, 400023, 400024, 
      400025, 400026, 400027, 400028, 400029, 400030, 400031, 400032, 
      400033, 400034, 400035, 400036, 400037, 400038, 400039, 400040, 
      400041, 400042, 400043, 400044, 400045, 400046, 400047, 400048, 
      400049, 400050, 400051, 400052, 400053, 400054, 400055, 400056, 
      400057, 400058, 400059, 400060, 400061, 400062, 400063, 400064, 
      400065, 400066, 400067, 400068, 400069, 400070, 400071, 400072, 
      400073, 400074, 400075, 400076, 400077, 400078, 400079, 400080, 
      400081, 400082, 400083, 400084, 400085, 400086, 400087, 400088, 
      400089, 400090, 400091, 400092, 400093, 400094, 400095, 400096, 
      400097, 400098, 400099, 400100, 400101, 400102, 400103, 400104, 
      400601, 400602, 400603, 400604, 400605, 400606, 400607, 400608, 
      400609, 400610, 400611, 400612, 400613, 400614, 400701, 400702, 
      400703, 400704, 400705, 400706, 400707, 400708, 400709, 400710, 
      401101, 401104, 401105, 401107, 401201, 401202, 401203, 401207, 
      401209, 401301, 401303, 421001, 421002, 421003, 421004, 421005, 
      421201, 421202, 421203, 421204, 421301, 421304, 421306) 
+0

能否請你解釋一下你需要在這裏找到什麼。因爲我認爲一個簡單的'select * from tab_name where col_name = key_to_search;' – codeomnitrix 2012-07-19 04:45:43

+0

範圍從哪裏來?他們在另一張桌子嗎? – 2012-07-19 07:17:34

+0

亞歷克斯poole沒有範圍不是來自另一個表用戶提供了我的xls表,其中範圍給出 – 2012-07-19 08:48:37

回答

1

除了作爲乏味,in有1000個值,如果你有大範圍你可能會觸及的極限。

如果你能得到的Excel數據導入的格式,你可以生成文本,形成CTE,你也許可以做的公式,你可以做這樣的事情:

with tmp_tab as (
    select 400001 as range_from, 400104 as range_to from dual 
    union all select 400601, 400614 from dual 
    union all select 400701, 400710 from dual 
    union all select 401101, null from dual 
    union all select 401104, null from dual 
    union all select 401105, null from dual 
    union all select 401107, null from dual 
    union all select 401201, null from dual 
    union all select 401202, null from dual 
    union all select 401203, null from dual 
    union all select 401207, null from dual 
    union all select 401209, null from dual 
    union all select 401301, null from dual 
    union all select 401303, null from dual 
    union all select 421001, 421005 from dual 
    union all select 421201, null from dual 
    union all select 421202, null from dual 
    union all select 421203, null from dual 
    union all select 421204, null from dual 
    union all select 421301, null from dual 
    union all select 421304, null from dual 
    union all select 421306, null from dual 
) 
select st.id, st.data 
from some_table st 
join tmp_tab tt on st.id between tt.range_from 
    and nvl(tt.range_to, tt.range_from); 

所以你打造一個基於Excel數據的公用表格表達式,範圍從-from和range-to;如果您只有一個數字,您可以將範圍保留爲空並稍後使用nvl將其作爲虛擬範圍;然後在表格中查找任何範圍內的數據。

棘手的一點是生成CTE,但即使是手動從數據手動執行,也比將每個範圍手動擴展到所有單個值更容易。

0

我不認爲這是可能的使用,但您可以將數據插入到一個臨時表或表變量:

DECLARE @table TABLE (value int) 
INSERT INTO @table VALUES (400001),(400002),(400003),(400004),(400005),(400006),(400007),(400008),(400009),(400010),(400011),(400012),(400013),(400014),(400015),(400016),(400017),(400018),(400019),(400020),(400021),(400022),(400023),(400024),(400025),(400026),(400027),(400028),(400029),(400030),(400031),(400032),(400033),(400034),(400035),(400036),(400037),(400038),(400039),(400040),(400041),(400042),(400043),(400044),(400045),(400046),(400047),(400048),(400049),(400050),(400051),(400052),(400053),(400054),(400055),(400056),(400057),(400058),(400059),(400060),(400061),(400062),(400063),(400064),(400065),(400066),(400067),(400068),(400069),(400070),(400071),(400072),(400073),(400074),(400075),(400076),(400077),(400078),(400079),(400080),(00081),(400082),(400083),(400084),(400085),(400086),(400087),(400088),(400089),(400090),(400091),(400092),(400093),(400094),(400095),(400096),(400097),(400098),(400099),(400100),(400101),(400102),(400103),(400104),(400601),(400602),(400603),(400604),(400605),(400606),(400607),(400608),(400609),(400610),(400611),(400612),(400613),(400614),(400701),(400702),(400703),(400704),(400705),(400706),(400707),(400708),(400709),(400710),(401101),(401104),(401105),(401107),(401201),(401202),(401203),(401207),(401209),(401301),(401303),(421001),(421002),(421003),(421004),(421005),(421201),(421202),(421203),(421204),(421301),(421304),(421306) 

DECLARE @minValue INT 
SET @minValue = (SELECT MIN (value) FROM @table) 

DECLARE @valueToTest INT 
SET @valueToTest = 200 

PRINT CASE WHEN @valueToTest < @minValue THEN 'below range' 
      ELSE 'NOT below range' 
     END 
+0

codeomnitrix假設我有範圍400001 - 400104,400601 - 400614和421001 - 421005如果我使用IN功能比我必須寫pin代碼從400001到400104比意味着我不得不手動編寫它100個引腳代碼是否有任何容易的是在沒有使用IN函數的情況下找到下面給出的範圍內的數據 – 2012-07-19 04:57:19

+0

nunes7452我只是想找到所有數據,使用IN函數,即使我已經得到正確的輸出,但是寫入100個pincode是繁瑣的方法。所以我需要簡單的方法來解決上述問題 – 2012-07-19 05:00:58

+0

這個問題被標記爲[oracle],所以這個語法是無效的。 – 2012-07-19 09:53:25

相關問題