Einzelnen Beitrag anzeigen

Elvis

Registriert seit: 25. Nov 2005
Ort: München
1.909 Beiträge
 
Delphi 2010 Professional
 
#14

Re: Optimierung einer SQL-Abfrage

  Alt 9. Okt 2008, 14:18
Da gibt es sicherlich 1.000 Möglichkeit um das in PL/SQL zu lösen.
Eine Lösung wäre es, die verkürzten Nummern in die Ergebnissmenge zu bekommen:
SQL-Code:
create or replace package BarntisNummernDings is
  type TAproxNumber is record(
     RemovedDigits Integer
    ,PartialValue Integer);

  type TAproxNumberList is table of TAproxNumber;

  function GetAproxNumbers(input in Integer) return TAproxNumberList
    pipelined;
end BarntisNummernDings;
/
create or replace package body BarntisNummernDings is

  function GetAproxNumbers(input in Integer) return TAproxNumberList
    pipelined is
    Item TAproxNumber;
    inputAsString VarChar(50);
  begin
    if input is null then
      return;
    end if;
  
    Item.PartialValue := input;
    Item.RemovedDigits := 0;
    pipe row(Item);
  
    if input < 10 then
      return;
    end if;
  
    inputAsString := input;
    if Length(inputAsString) > 1 then
      for i in 1 .. Length(inputAsString) - 1 loop
        Item.RemovedDigits := Item.RemovedDigits + 1;
      
        Item.PartialValue := SubStr(inputAsString, 1, Length(inputAsString) - i);
        pipe row(Item);
      
        Item.PartialValue := SubStr(inputAsString, i + 1);
        pipe row(Item);
      end loop;
    end if;
    return;
  end;
end BarntisNummernDings;
/
SQL-Code:
SELECT *
FROM table(BarntisNummernDings.GetAproxNumbers(12345))
Code:
0   12345
1   1234
1   2345
2   123
2   345
3   12
3   45
4   1
4   5
SQL-Code:
SELECT aproxIn.RemovedDigits
      ,aproxT.RemovedDigits
      ,t.*
FROM table(BarntisNummernDings.GetAproxNumbers(12345)) aproxIn
      ,deineTabelle t
      ,table(BarntisNummernDings.GetAproxNumbers(t.DeinWert )) aproxT
WHERE aproxIn.PartialValue = aproxT.PartialValue
ORDER BY aproxIn.RemovedDigits
         ,aproxT.RemovedDigits
         ,...
Robert Giesecke
I’m a great believer in “Occam’s Razor,” the principle which says:
“If you say something complicated, I’ll slit your throat.”
  Mit Zitat antworten Zitat