sql - 查找 2 个相似表之间的差异(计数差异)
问题描述
我有 2 个类似的表,一个是每月更新的实时表,另一个是相同的数据,但有一个snapshot
表,如显示特定数据时的数据,还有一个额外的列Live_History_Month
调用确定数据进来的month_year。
我正在尝试查看特定列的计数是否有任何变化,以便我可以进一步调查。我创建了以下 SQL 代码
select codetoinvestigate
,Year_Month
,count(*)
from tbl1
where Live_History_Month = Year_Month
group by codetoinvestigate
,Year_Month
select codetoinvestigate
,Year_Month
,count(*)
from tbl2
group by codetoinvestigate
,Year_Month
现在我如何链接这些,以便我可以查看指定列的计数之间是否有任何差异,以便我可以做出需要调查的明智决定。
只是为了确认 tbl1 是快照表,而 tbl2 是当前最新数据的表。
谢谢
解决方案
这将返回不同的计数和缺失的代码
with t1 as
(
select codetoinvestigate
,Year_Month
,count(*) as cnt
from tbl1
where Live_History_Month = Year_Month
group by codetoinvestigate
,Year_Month
),
t2 as
(
select codetoinvestigate
,Year_Month
,count(*) as cnt
from tbl2
group by codetoinvestigate
,Year_Month
)
select coalesce(t1.codetoinvestigate, t2.codetoinvestigate)
,coalesce(t1.Year_Month, t2.Year_Month)
,t1.cnt
,t2.cnt
,case when t1.codetoinvestigate is null
then 'missing code t1'
when t2.codetoinvestigate is null
then 'missing code t2'
when t1.cnt <> t2.cnt
then 'count different'
etc...
end
from t1 full join t2
on t1.codetoinvestigate = t2.codetoinvestigate
and t1.Year_Month = t2.Year_Month
and t1.cnt <> t2.cnt
要仅检查不同的计数,请切换到内部联接
推荐阅读
- flutter - SingleSChildScrollView 在 IOS 中向上移动
- python - 未定义符号:cblas_dtrsm
- tar - 如何在没有完整路径的情况下附加 tar
- php - PHP/MySQL 统计每个用户的记录
- java - 返回列表中一个或多个元素匹配字段的所有元素的查询 Java MongoDB Driver 3.7.1
- amazon-rekognition - AWS Rekognition 将人脸与文件夹中的图像进行比较
- sql - 无法在 oracle 架构上查询表
- android - Android libVLC - 停止播放后清除表面的方法
- android - 如何杀死在 Android 的 Scheduler.computation() 中运行的繁重进程(TensorFlow Lite)
- php - 与 & 字的精确正则表达式匹配