Link to home
Start Free TrialLog in
Avatar of tnowacoski
tnowacoski

asked on

oracle - complex dynamic computations

I have a very complex logic stream where I need to read in detail data, filter it, and then count, sum, or further filter based on values in certain columns of a "rules" record in another table.  This cannot be done in a single pass and since it is dynamic, I cannot use a static view.  My initial thought was to retrieve the detail data from a pipelined function so that further "selects" can be performed.

1) Is this the best approach?
2) How would I write the pipelined function?  The only examples I see do not "select" from a table.
Avatar of Jacobfw
Jacobfw
Flag of Canada image

This appears to be the perfect instance for using Dyanmic SQL Statements within PL/SQL allowing you to create dynamic SQL based on query results, filters and additional criteria.

http://download.oracle.com/docs/cd/B10500_01/appdev.920/a96590/adg09dyn.htm
Avatar of schwertner
If the rules are complex you can use a temporary table within a transaction and prepare the selected rows there in the first pass. After that in the transaction you can anlayze the selected rows and to use further filters.
if the filters are complex try to use PL/SQL procedure because PL/SQL as procedural language is very flexible and can implement very complex logics.
If the filters are not very complex you can use Dynamic SQL like Jacobfw proposed. SQL is not so flexible as PL/SQL.
Avatar of tnowacoski
tnowacoski

ASKER

schwertner: by temporary tables do you mean
 create global temporary table tablename ...

Open in new window

These are statically defined and dynamically populated..right?
What if I do not exactly know the structure of the table until execution?
You can use dynamic DML statements to create the temporary table....
or just create a generic table with generic columns and use as needed
In theory, can't I have a function that returns a data type of table that I then can select against?

Record data type:
create or replace type row_type AS OBJECT
    (site        varchar2(5), ... pcs number);

Open in new window


Table data type:
create or replace type table_type as 
	table of row_type;

Open in new window


Function to return table_type:
 FUNCTION get_detail_data(in_where IN VARCHAR2) RETURN table_type pipelined AS...

Open in new window


Procedure to call Function:
PROCEDURE get_rebates AS
  _data table_type;
.
.
.
_data := get_detail_data('where info');
select count(*) from table(_data);

Open in new window

Jacobfw:  Sort of but in the
...anonymous block that calls the myFunction function, gets a table of results...
I would want to call a select:

example:
 
  _data table_type;
.
.
.
_data := get_detail_data('where info');
select count(*) from table(_data);

Open in new window

PL/SQL array is aray that is kept in the memory (PGA).
Temporary table is kept on the disk and can have many columns.
The temporary table is created in the live of an transaction or sesion and is deleted automatically.

You can create dynamically tables in the lifecycle of the PL/SQL program. And to drop them. Keeping data in tables has the advantage to be the native data structure for the database and to have many programatic tools and construction to process and to keep the data.

But the main thing is to keep the construction as simple as possible. complex construction are hard for processing and for performance tuning.
any examples for the dynamic tables?
If I am not mistaken, that is creating a table, not a dynamic temporary table.  
The purpose of this code is to dynamically select a subsection of data from an existing table that will be used for detail information.  This sub-selection is dynamic in the sense that the "where" criteria will change from pass to pass.  This sub-selection needs to be stored in a temporary location (array, temp table, etc) to have further processing done to get various "summary" calculations done.  I am looking for a method that will be processor friendly, will clean up when done, and offer the greatest flexibility to be dynamic.
Will it help if I bump up the points?
I do not believe you can bump up the points past 500.
I believe the Experts have provided enough guidance to your original request for an approach.  Without a fully developed work scope, we can't write the code for you and determine the best approach for your application, environment etc.

Maybe you could reword your question with more specifics.
I just bumped the point from 250pts to 500pts.  So far, I think this thread has narrowed the option down to Temporary Tables and I am assuming that this would be the Global Temporary Table.  But from my understanding, this table is statically defined and dynamically populated...Correct?
If so, this brings me back to my earlier questions of: How would one dynamically create this table and populate it during the life of the session, how can it be dynamically truncated during the session and does anyone have sample code where a Global Temporary Table is dynamically created, populated, used within a "select" statement, and then destroyed at the end of the session?
ASKER CERTIFIED SOLUTION
Avatar of Jacobfw
Jacobfw
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial