我是SQL和PLSQL的新手。我认为有一种方法可以加入这两个查询,但我很难过。它们是完全相同的,除了第一个查询找到一个不同的e.identifying_party,说&#34; 5111&#34;,然后第二个查找使用e.identifying_party =&#34; 5111&#34;。< / p>
SELECT DISTINCT e.identifying_party
FROM voucher b,
cc_authorization d,
case e,
episode g,
invoice c,
invoice_item f,
svcperiod_info v
WHERE b.cc_authorization_id = d.cc_authorization_id
AND d.subsidy_id = g.episode_id
AND g.case_id = e.case_id
AND b.voucher_id = f.voucher_id
AND f.invoice_id = c.invoice_id
AND v.svcperiod_id = c.svcperiod_id
AND f.status = 8
AND f.net_amount > 0
AND (v.end_dt >= '01-APR-17'
AND v.end_dt < '02-APR-17')
AND e.case_id <4000;
第二个查询完全相同,但使用了e.identifying_party的输入:
SELECT DISTINCT b.child_id
FROM voucher b,
cc_authorization d,
case e,
episode g,
invoice c,
invoice_item f,
svcperiod_info v
WHERE b.cc_authorization_id = d.cc_authorization_id
AND d.subsidy_id = g.episode_id
AND g.case_id = e.case_id
AND b.voucher_id = f.voucher_id
AND f.invoice_id = c.invoice_id
AND v.svcperiod_id = c.svcperiod_id
AND f.status = 8
AND f.net_amount > 0
AND (v.end_dt >= '01-APR-17'
AND v.end_dt < '02-APR-17')
AND e.identifying_party = 5111
AND e.case_id <4000;
另外,我正在调试别人的代码,所以如果有任何其他编辑或缺少信息,请告诉我。谢谢!
答案 0 :(得分:0)
加入的一种方式是:
if ( typeof window != 'undefined' )
请注意,我在表Select distinct b.child_id
From voucher b, cc_authorization d, case e, episode g, invoice c, invoice_item f, svcperiod_info v
Where b.cc_authorization_id = d.cc_authorization_id and d.subsidy_id = g.episode_id and g.case_id = e.case_id
and b.voucher_id = f.voucher_id and f.invoice_id = c.invoice_id and v.svcperiod_id = c.svcperiod_id
and f.status = 8 and f.net_amount > 0 and (v.end_dt >= '01-APR-17' and v.end_dt < '02-APR-17') and e.case_id<4000 and e.identifying_party in (Select distinct e1.identifying_party
From voucher b, cc_authorization d, case e1, episode g, invoice c, invoice_item f, svcperiod_info v
Where b.cc_authorization_id = d.cc_authorization_id and d.subsidy_id = g.episode_id and g.case_id = e1.case_id
and b.voucher_id = f.voucher_id and f.invoice_id = c.invoice_id and v.svcperiod_id = c.svcperiod_id
and f.status = 8 and f.net_amount > 0 and (v.end_dt >= '01-APR-17' and v.end_dt < '02-APR-17') and e1.case_id<4000);
的子查询中使用了e1
别名。您可能需要在sub = query中为其他表使用不同的别名。
答案 1 :(得分:0)
根据其他人的建议以及您对查询的说法,这可能会有效:
SELECT DISTINCT v.child_id
FROM voucher v
INNER JOIN cc_authorization cca ON v.cc_authorization_id = cca.cc_authorization_id
INNER JOIN episode ep cca.subsidy_id = ep.episode_id
INNER JOIN case ca ON ep.case_id = ca.case_id
INNER JOIN invoice_item it ON d.voucher_id = it.voucher_id
INNER JOIN invoice inv ON it.invoice_id = inv.invoice_id
INNER JOIN svcperiod_info spi ON inv.svcperiod_id = spi.svcperdiod_id
WHERE it.status = 8
AND it.net_amount > 0
AND (spi.end_dt >= DATE'01-APR-17' AND spi.end_dt < DATE'02-APR-17')
AND ca.case_id < 4000
AND ca.identifying_party IN (
SELECT DISTINCT ca1.identifying_party
FROM voucher v
INNER JOIN cc_authorization cca ON v.cc_authorization_id = cca.cc_authorization_id
INNER JOIN episode ep cca.subsidy_id = ep.episode_id
INNER JOIN case ca1 ON ep.case_id = ca.case_id
INNER JOIN invoice_item it ON d.voucher_id = it.voucher_id
INNER JOIN invoice inv ON it.invoice_id = inv.invoice_id
INNER JOIN svcperiod_info spi ON inv.svcperiod_id = spi.svcperdiod_id
WHERE it.status = 8
AND it.net_amount > 0
AND (spi.end_dt >= DATE'01-APR-17' AND spi.end_dt < DATE'02-APR-17')
AND ca.case_id < 4000
);
注意我是如何更改连接以使用ANSI标准连接并为每个表提供更合适的别名。
但是,如果您可以提供有关数据的更多信息,我认为可以改进。为什么我们需要在内部和外部查询中运行相同的逻辑?它们可以合并到同一个查询中(例如SELECT DISTINCT v.child_id,ca1.identifying_party ......)?
答案 2 :(得分:0)
由于查询是相同的,除了您要拔出的特定字段,然后逻辑上查询返回您需要的所有数据 - 您只需要求它:
SELECT DISTINCT e.identifying_party, b.child_id
FROM voucher b
INNER JOIN cc_authorization d
ON d.cc_authorization_id = b.cc_authorization_id
INNER JOIN episode g
ON g.subsidy_id = d.episode_id
INNER JOIN case e
ON e.case_id = g.case_id
INNER JOIN invoice_item f
ON f.voucher_id = b.voucher_id
INNER JOIN invoice c
ON c.invoice_id = f.invoice_id
INNER JOIN svcperiod_info v
ON v.svcperiod_id = c.svcperiod_id
WHERE f.status = 8 AND
f.net_amount > 0 AND
v.end_dt >= '01-APR-17' AND
v.end_dt < '02-APR-17' AND
e.case_id < 4000
ORDER BY IDENTIFYING_PARTY, CHILD_ID
在这里我重写了查询以使用“现代”连接(SQL-92 - 25年“新” - 我的,时间如何飞:-)但原始查询应该可以正常工作。
这将为您提供IDENTIFYING_PARTY和CHILD_ID的不同组合。我添加了一个ORDER BY以确保以合理的顺序返回结果。
祝你好运。