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