0
从 own.no_preselection_1_a 中选择 COUNT(1);
SELECT COUNT(1) FROM own.no_preselection_1;

选择计数(1)从
  (SELECT DISTINCT * FROM own.no_preselection_1_a
  );

选择计数(1)从
  (SELECT DISTINCT * FROM own.no_preselection_1
  );

选择计数(1)
FROM OWN.no_preselection_1 t1
INNER JOIN OWN.no_preselection_1_a t2
ON t1.number = t2.number
AND t1.location_number = t2.location_number;

这将返回:

计数(1)               
----------------------
398                    

计数(1)               
----------------------
398                    

计数(1)               
----------------------
308                    

计数(1)               
----------------------
308                    

计数(1)               
----------------------
578                    

如果我们在这里查看连接的视觉解释:http: //www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

问题出在最后一个查询上。我会认为如果集合是相同的(即完美重叠),那么内部连接将返回一个与原始集合大小相同的集合。

问题是每个重复项在为彼此创建条目吗?(例如,如果每个表上有 3 个相同值的重复值,它会为它创建 3x3 = 9 个条目?)这里的解决方案是什么?(只需选择不同的值来进行内部连接?)这是检查两个表是否包含相同数据的好测试吗?

4

1 回答 1

1

您的表中有重复项,因为列表中的第一个和第三个以及第二个和第四个计数清楚地表明了这一点。

连接正常工作,所以没有“问题”。你想达到什么目的?加入不满足您的目标。

我建议你用一些实际数据和你想要的结果来注释你的问题。

如果要显示两个表具有相同的值,可以尝试联合。假设两个表中的所有列都相同,并且一行中的列唯一标识每一行:

select t.*
from ((select '1' as which, t.*
       from OWN.no_preselection_1 t
      ) union all
      (select '1-a' as which, t.*
       from OWN.no_preselection_1_a
      )
     ) t
group by < all the columns in the tables >
having count(*) <> 1

如果您仅限于两列并想查看是否有相应的条目(有重复项),则以下工作:

select t.*
from ((select '1' as which, number, location_number,
              row_number() over (partition by number, location_number order by number) as seqnum
       from OWN.no_preselection_1 t
      ) union all
      (select '1-a' as which, number, location_number,
              row_number() over (partition by number, location_number order by number) as seqnum
       from OWN.no_preselection_1_a
      )
     ) t
group by number, location_number, seqnum
having count(*) <> 1
于 2013-02-20T01:58:18.477 回答