一个SQL语句产生三个表
问题描述:
我需要创建一个将生成三个表的sql语句。一个SQL语句产生三个表
我有,如果我单独运行,我可以产生三个表的单个SQL代码: 表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
表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)
所以表2平它的输入从列表3从表2中列出它的输入。
如何重新分解这些SQL语句以删除代码重复并轻松地将p将列值分配到下一个表格。
是否可以将这些SQL转换为一个?
编辑:Netezza公司DB查询通过DBVisualizer中
答
当然可以!可能有助于确切知道你需要哪些列,但我相信下面的工作。噢,总是明确限定您的连接,而不是使用隐式连接语法(逗号分隔的FROM
子句),这样更好。
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
不知道更多关于您的布局,很难知道还有什么可能被淘汰。
有两点要注意:
- 我假设
Eq_Mo_Trade_Charge.charge_amount
和Eq_Mo_Allocation_Detail_Charge.charge_amount
是小数/钱的类型,而不是某种浮点/实型的。如果他们是花车,不仅条件不成立,你也不会储存确切的金额。 - 所有以
Eq_Mo_...
为前缀的“噪音”都有点磨损。 Netezza不支持可以放置表格的模式吗?
创建两个视图? – deerchao
将它添加到**标签** - 这是阐明你使用的数据库引擎之类的东西的地方:-) –
@deerchao,我很新的SQL我怎么能创建这些视图,从列传递值一个到另一个像变量 – Will