手头问题的简化设置。
表A包含列rz_id和sHashA。表A非常大。
表B包含scode和sHashB列。可以有许多sHashB值 对应于特定的scode值。表B相对较多 比表A小。
对于每个scode值(大约200个),我必须执行如下查询(在这种情况下scode为500)。
select count(distinct rz_id) from A where substr(sHashA, 1, 5) in (select substr(sHashB, 1, 5) from B where scode = 500);
对于每个scode值,我写一个像上面这样的查询,这样我最终会得到200个查询
select count(distinct rz_id) from A where substr(sHashA, 1, 5) in (select substr(sHashB, 1, 5) from B where scode = 500);
select count(distinct rz_id) from A where substr(sHashA, 1, 5) in (select substr(sHashB, 1, 5) from B where scode = 501);
select count(distinct rz_id) from A where substr(sHashA, 1, 5) in (select substr(sHashB, 1, 5) from B where scode = 502);
.
.
.
select count(distinct rz_id) from A where substr(sHashA, 1, 5) in (select substr(sHashB, 1, 5) from B where scode = 700);
问题是这最终会超过大桌子200次 这很费时间。我希望能够实现这一目标 单通(单一查询)。
我想要创建一个包含表A和许多行的表 通过类似
的查询将其他列作为表B.select /*+ streamtable(a) */ a.*, if(substr(sHashA, 1, 5) in (select
substr(sHashB, 1, 5) from B where scode = 500, 1, 0) as scode_500,
if(substr(sHashA, 1, 5) in (select substr(sHashB, 1, 5) from B where
scode = 501, 1, 0) as scode_501, ... if(substr(sHashA, 1, 5) in
(select substr(sHashB, 1, 5) from B where scode = 700, 1, 0) as
scode_700 from A a;
这将在对应于表A的每行scode的200列中的每一列中输出0或1.稍后我可以总结列以获得计数。由于我也有兴趣估计任何两个scodes之间的计数重叠,我想到了上表。
但我得到解析错误,我怀疑内部不允许查询 IF声明。
最后的问题是:我如何将所有这些查询减少到单个查询中,以便最终只通过一次巨大的表行?还请建议处理此计数的其他方法,记住我也有重叠的内容。
答案 0 :(得分:3)
这样的事情怎么样;
select count(distinct A.rz_id), B.scode
from A,B
where substr(A.sHashA, 1, 5) = substr(B.sHashB, 1,5)
and B.scode in (500,501,...)
group by B.scode
单次传递获取所有数据