0
create table CIT_ADMIN.OBJECT_DETAIL_MASTER (
  RULE_ID   VARCHAR(10), 
  ATTR_1   VARCHAR(50), 
  ATTR_2   VARCHAR(50), 
  ATTR_3   VARCHAR(50), 
  CREATED_DATE    DATE DEFAULT SYSDATE)

create table CIT_ADMIN.PRIORITY_RULE_FACTORY (
  RULE_ID   VARCHAR(10), 
  RULE_DESC VARCHAR(10), 
  RULE_1   VARCHAR(50), 
  RULE_2   VARCHAR(50), 
  RULE_3   VARCHAR(50), 
  CREATED_DATE    DATE DEFAULT SYSDATE)

insert into CIT_ADMIN.PRIORITY_RULE_FACTORY
       (RULE_ID, RULE_DESC,  RULE_1,         RULE_2,          RULE_3)
values ('1',     'POSITION', 'ODS_KEY',      'NOTIONAL',      'SIK')

insert into CIT_ADMIN.PRIORITY_RULE_FACTORY
       (RULE_ID, RULE_DESC,  RULE_1,         RULE_2,          RULE_3)
values ('2',     'CLIENT',   'UCN',          'AGREEMENT_ID',  'FACILITY_ID')

insert into CIT_ADMIN.PRIORITY_RULE_FACTORY
       (RULE_ID, RULE_DESC,  RULE_1,         RULE_2,          RULE_3)
values ('3',     'NETTING',  'AGREEMENT_ID', 'AGREEMENT_AMT', 'MATURITY')

insert into CIT_ADMIN.OBJECT_DETAIL_MASTER
       (RULE_ID, ATTR_1, ATTR_2, ATTR_3)
values ('1',     '123',  '1000', '600')

insert into CIT_ADMIN.OBJECT_DETAIL_MASTER
       (RULE_ID, ATTR_1, ATTR_2, ATTR_3)
values ('2',     '456',  '2000', '500')

insert into CIT_ADMIN.OBJECT_DETAIL_MASTER
       (RULE_ID, ATTR_1, ATTR_2, ATTR_3)
values ('3',     '789',  '3000', '400')

These are my tables. Rule id is foreign key in master table, also the primary key in rule factory table. I tried writing query that can link the rule with the data.

**Like:**
WITH ATTR AS (SELECT * FROM CIT_ADMIN.PRIORITY_RULE_FACTORY WHERE RULE_DESC = 'POSITION')

SELECT ATTR_2 FROM CIT_ADMIN.OBJECT_DETAIL_MASTER a
WHERE RULE_ID in (SELECT ATTR.RULE_ID FROM attr) 
AND ATTR_1 = '123'

If I know position and ods_key I want Notional. This query fetched me that but the problem is i dont know the position of those attributes. Can you guys please give me some pointers to move ahead.!!!

Regards.

6
  • I don't understand what you mean about the "position" of the attributes. Please edit your question and explain further. Thanks. (And BTW, here's an SQLFiddle containing the schema and query). Jul 23, 2013 at 10:53
  • @Bob : All i meant was, i will have information in the form of rules, as described in the rule table. When i say rule 1, it means position, as defined in rule description. and rule_1, rule_2, rule_3 are the attributes of position object, like - ods_key, notional, etc... whose values are present in master table. So, when i say it is a position object rule_id becomes 1. So for this type of object i need the notional value when ods_key = 123
    – Vivek
    Jul 23, 2013 at 11:02
  • So i need something that can map the column name of data(in this case Notional) from rule table, then read the corresponding value of column from master table.
    – Vivek
    Jul 23, 2013 at 11:04
  • I'm with Bob here, I don't 100% understand what you're after; there are no column names to map. Could you provide your expected output as well?
    – Ben
    Jul 23, 2013 at 11:43
  • The way to connect two tables are via the rule_id. The master table is basically a table to hold unstructured data. Another way to visualize it is to consider: ATTR_1 maps to rule_1, ATTR_2 maps to rule_2, and similarly for others. In this way I dont have to create one master table with multiple columns where lot of columns are null.
    – Vivek
    Jul 23, 2013 at 11:49

2 Answers 2

1

I think this is a pretty ropey design. I mean, there must be something wrong with a model when the notoriously poor EAV pattern appears like a sensible alternative.

Anyway, here is a function which resolves the task in hand: I suggest a function simply because Rules Engines are tricky beasts, and encapsulation will serve you well when it comes to revising your functionality.

create or replace function eval_rule
    ( p_rule_id in priority_rule_factory.rule_id%type
      , p_rule in priority_rule_factory.rule_1%type )
     return object_detail_master.attr_1%type
is
    return_value object_detail_master.attr_1%type;
begin

     select case 
            when prf.rule_1 = p_rule then odm.attr_1
            when prf.rule_2 = p_rule then odm.attr_2
            when prf.rule_3 = p_rule then odm.attr_3
            end
    into return_value
    from priority_rule_factory prf
         join object_detail_master odm
            on prf.rule_id = odm.rule_id
    where prf.rule_id = p_rule_id;        

    return return_value;
end eval_rule;
/
1

You might find it easier to work with if you normalize the "rules" and "attributes" - create a table which holds a single attribute name (e.g. 'ODS_KEY', 'NOTIONAL', etc) and the value which goes with it (e.g. 123, 1000, etc) and relate this to a rule (e.g. 'POSITION', 'CLIENT', 'NETTING', etc).

Share and enjoy.

1
  • My requirement is more on the Meta-Data driven side. So I am thinking of using a columnar DB instead. Still appreciate your help. Thanks a lot.
    – Vivek
    Jul 30, 2013 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.