假设我有一个下面给出的范围
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 :(得分: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-range;你只有一个数字,你可以将范围保留为空,然后使用nvl
使其成为虚拟范围;然后在表格中查找任何范围内的数据。
棘手的一点是生成CTE,但即使从您拥有的数据中手动执行此操作也会比手动将每个范围扩展到所有单个值更容易。
答案 1 :(得分:0)
我认为不可能使用IN,但您可以将数据插入临时表或表变量中:
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