2016-10-13 12 views
0

を比較すると、私は次のように値を持つフィールドRANGEを持つテーブルがあります。のOracle SQL - 重複した値

102453 
104953-256454 

値が区切らシングル6桁の数字または2つの6桁の数字可能にダッシュ。

ユーザは、aの値(6桁の数字または2桁の6桁の数字をダッシュ​​で区切ったもの)を入力し、その値をデータベースの既存の値と比較する必要があります。

  • 入力された値が6桁の数字である場合、それはデータベース内のすべての単一の6桁の値を、またによって分離された2桁の値として格納されている別個の6桁の値と比較されるべきです( - )
  • 入力された値が2桁の6桁の数値の場合、解析する必要があり、各値をデータベースの各値と比較する必要があります。

どうすればよいですか?

+0

代わりに2つの列を使用してください。 (テーブルを変更できない場合は、ビュー、派生テーブルまたはcteを使用できます) – jarlh

+0

これは残念なことにクライアントの設計です。 –

+1

REGEXP_SUBSTR(s、 '[^ - ] +'、1,1)とREGEXP_SUBSTR(s、 '[^ - ] +'、1,2)を使用して、入力から6桁の値を取得し、 LIKE演算子を使用します。 – hemalp108

答えて

0

私はある種のIDフィールドがあると思います(希望します)。

with Dumbassery as 
(
select id, 'Single' as NumType, substr(MyField,1,6) as Num1, '' as Num2 
from MyTable 
where length(MyField) = 6 
union 
select id, 'Range' as NumType, substr(MyField,1,6) as Num1, substr(MyField,8,6) as Num2 
from MyTable 
where length(MyField) = 13 
) 

, Filter1 as 
(
select id, 
     case 
      when exists (select 1 from Dumbassery D2 where D1.Num1 between D2.Num1 and D2.Num2 and D2.NumType = 'Range') then 'Exists' 
      when exists (select 1 from Dumbassery D2 where D2.NumType = 'Single' and D1.Num1 = D2.Num1) then 'Exists' 
      else 'New' 
     end as NumExist 
from Dumbassery D1 
where D1.NumType = 'Single' 

union 

select id, 
     case 
      when exists (select 1 from Dumbassery D2 where D1.Num1 between D2.Num1 and D2.Num2 and D2.NumType = 'Range') then 'Exists' 
      when exists (select 1 from Dumbassery D2 where D1.Num2 between D2.Num1 and D2.Num2 and D2.NumType = 'Range') then 'Exists' 
      when exists (select 1 from Dumbassery D2 where D2.Num2 between D1.Num1 and D1.Num2 and D2.NumType = 'Single') then 'Exists' 
      else 'New' 
     end as NumExist 
from Dumbassery D1 
where D1.NumType = 'Range' 
) 

select distinct * 
from Filter1 
where NumExist = 'New' 
0

名前のついた範囲にフィールドを設定すると、新しい範囲で交差を見つける必要があると思います。テーブルtest_valを追加して、new_rangeのバリエーションを確認します。

WITH 
table_ranges AS /*is table with ranges*/ 
(select '123212' as range from dual union all 
    select '123214-223214' as range from dual union all 
    select '123900-987121' as range from dual) 
,test_val as /*is table with test values*/ 
(select '123212' as test_range from dual union all 
    select '123213' as test_range from dual union all 
    select '123215' as test_range from dual union all 
    select '123213-123290' as test_range from dual union all 
    select '124000-125000' as test_range from dual union all 
    select '987000-987124' as test_range from dual union all 
    select '987122-987124' as test_range from dual) 
    select CAse WHEN EXISTS (select null 
          from table_ranges 
         where substr(t.test_range, 1,6) between substr(range, 1,6) and substr(range, -6,6) 
          or substr(t.test_range, -6,6) between substr(range, 1,6) and substr(range, -6,6)) 
       THEN 'already exists' 
       ELSE'intersection not found' 
      END AS test_status 
    FROM test_val t