0

I need to create a sql statement which will produce three tables.

I have the individual SQL code which if I run individually I can produce the three tables: Table 1

select t.TRADE_ID, t.TRADE_VERSION, t.TRADE_SOURCE_SYSTEM, tl.LINK_PARENT_ID, 
tl.LINK_PARENT_VERSION, tc.CHARGE_AMOUNT, tc.CHARGE_SCOPE FROM EQ_MO_TRADE (NOLOCK) t, EQ_MO_TRADE_CHARGE (NOLOCK) tc, EQ_MO_TRADE_LINKAGE (NOLOCK) tl 
WHERE t.TRADE_OID = tl.TRADE_OID
AND t.TRADE_OID = tc.TRADE_OID
AND tl.LINK_TYPE = 'Allocation'
AND tc.CHARGE_NAME = 'Commission'
AND tc.CHARGE_AMOUNT != 0.0

Table 2

   select aD.ALLOCATION_ID, aD.ALLOCATION_VERSION, aD.ALLOCATION_SOURCE_SYSTEM, 
    al.LINK_PARENT_ID, al.LINK_PARENT_VERSION, ac.CHARGE_AMOUNT FROM EQ_MO_ALLOCATION_DETAIL aD, EQ_MO_ALLOCATION_DETAIL_CHARGE ac, EQ_MO_ALLOCATION_INSTR_LINKAGE al, EQ_MO_ALLOCATION_INSTR aI 
    WHERE  aD.ALLOCATION_DETAIL_OID = ac.ALLOCATION_DETAIL_OID
    AND aD.ALLOCATION_INSTR_ID = aI.ALLOCATION_INSTR_ID
    AND aI.ALLOCATION_INSTR_OID = al.ALLOCATION_INSTR_OID
    AND ac.CHARGE_NAME = 'Commission'
    AND ac.CHARGE_AMOUNT != 0.0
    AND ac.CHARGE_AMOUNT != -1.0
    and aD.ALLOCATION_ID in 

 (select tl.LINK_PARENT_ID FROM EQ_MO_TRADE t, EQ_MO_TRADE_CHARGE tc, EQ_MO_TRADE_LINKAGE tl 
    where t.TRADE_OID = tl.TRADE_OID
    AND t.TRADE_OID = tc.TRADE_OID
    AND tl.LINK_TYPE = 'Allocation'
    AND tc.CHARGE_NAME = 'Commission'
    AND tc.CHARGE_AMOUNT != 0.0) 

So table 2 draws it's input from a column in table 1. Table 3 draws it's input from a column in table 2.

How can I re-factor these SQL statements to remove the Code duplication and easily pass the column values to the next table.

Is it possible to roll these SQL into one?

EDIT: Netezza DB querying through DBVisualizer

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Will
  • 8,246
  • 16
  • 60
  • 92
  • Added it to the **tags** - that's the place to spell out things like the database engine you use :-) – marc_s Jan 17 '13 at 16:36
  • @deerchao, I'm pretty new to SQL how could I create these views, passing the column values from one to the other like variables – Will Jan 17 '13 at 16:37

1 Answers1

0

Sure you can! Might help to know exactly which columns you need, but I believe the following should work. Oh, it's considered better to always explicitly qualify your joins, rather than using the implicit-join syntax (comma-separated FROM clause).

WITH Charged_Allocated_Commission (trade_id, trade_version, trade_source_system, link_parent_id, link_parent_version, charge_amount, charge_scope) AS
     SELECT t.trade_id, t.trade_version, t.trade_source_system,
            tl.link_parent_id, tl.link_parent_version, 
            tc.charge_amount, tc.charge_scope
     FROM Eq_Mo_Trade t
     JOIN Eq_Mo_Trade_Linkage tl
       ON tl.trade_oid = t.trade_oid
          AND tl.link_type = 'Allocation'
     JOIN Eq_Mo_Trade_Charge tc
       ON tc.trade_oid = t.trade_oid
          AND tc.charge_name = 'Commission'
          AND tc.charge_amount != 0.0)
SELECT cac.trade_id, cac.trade_version, cac.trade_source_system, 
       cac.link_parent_id, cac.link_parent_version, 
       cac.charge_amount, cac.charge_scope,
       ad.allocation_version, ad.allocation_source_system,  
       -- I'm fairly certain these are duplicates of cac.trade_version/_source_system...
       al.link_parent_id, al.link_parent_version,
       ac.charge_amount
FROM Charged_Allocated_Commission cac
JOIN Eq_Mo_Allocation_Detail ad
  ON ad.allocation_id = cac.link_parent_id
JOIN Eq_Mo_Allocation_Detail_Charge ac
  ON ac.allocation_detail_oid = ad.allocation_detail_oid
     AND ac.charge_name = 'Commission'
     AND ac.charge_amount NOT IN (0.0, -1.0)
JOIN Eq_Mo_Allocation_Instr ai
  ON ai.allocation_instr_id = ad.allocation_instr_id
JOIN Eq_Mo_Allocation_Instr_Linkage al
  ON al.allocation_instr_oid = ai.allocation_instr_oid

Without knowing a little more about your layout, it's hard to know what else could potentially be eliminated.
A couple of notes:

  • I'm assuming that Eq_Mo_Trade_Charge.charge_amount and Eq_Mo_Allocation_Detail_Charge.charge_amount are decimal/money types, and not some sort of float/real type. If they are floats, not only will the conditions not work, you won't be storing exact amounts either.
  • The 'noise' of having everything prefixed with Eq_Mo_... is a little wearing. Does Netezza not support schemas that tables could be placed in instead?
Clockwork-Muse
  • 12,806
  • 6
  • 31
  • 45