我必须比较两个表并找到两个不同数据库中两个表之间缺少的记录。 (考虑到tableA和tableB中记录应该相同的理想情况)。 tableA_count和tableB_count是Select count(*)的结果。 首先,我尝试了下面提到的方法,并按预期得到了结果。
if (tableA_count > tableB_count) {
query1 <- paste0("SELECT * FROM tableA (NOLOCK)")
results1 <- sqlQuery(cn_ods, query)
query2 <- paste0("SELECT * FROM tableB (NOLOCK)")
results2 <- sqlQuery(cn_ad, compare16_2_join)
test_anti_join <- anti_join(results1, results2, by=c("CLIENT_ID"="ClientID")
rm(results1)
rm(results2)
View(head(test_anti_join))
} else if (tableA_count < tableB_count) {
query1 <- paste0("SELECT * FROM tableA (NOLOCK)")
results1 <- sqlQuery(cn_ods, query)
query2 <- paste0("SELECT * FROM tableB (NOLOCK)")
results2 <- sqlQuery(cn_ad, compare16_2_join)
test_anti_join <- anti_join(results2, results1, by=c("ClientID"= "CLIENT_ID")
rm(results1)
rm(results2)
View(head(test_anti_join))
}
但是,保持精神高度并为我的代码添加一些价值,我试图为它编写一个函数。经过多次尝试,这是我最准确的代码,但仍然无法从中返回任何值。
Fun_Display_Missing_Recrods <- function(ods_count, app_count,ods_t, app_t, col_a, col_b) {
if (ods_count > app_count) {
query1 <- paste0("SELECT * FROM ", ods_t ," (NOLOCK)")
results1 <- sqlQuery(cn_ods, query1)
query2 <- paste0("SELECT * FROM ",app_t," (NOLOCK)")
results2 <- sqlQuery(cn_ad, query2)
test_anti_join1 <- anti_join(results1, results2, by= c(col_a = col_b))
rm(query1)
rm(query2)
rm(results1)
rm(results2)
# Records not in tables ##
View(test_anti_join1)
} else if (ods_count < app_count) {
query1 <- paste0("SELECT * FROM ",ods_t," (NOLOCK)")
results1 <- sqlQuery(cn_ods, query1)
query2 <- paste0("SELECT * FROM ",app_t," (NOLOCK)")
results2 <- sqlQuery(cn_ad, query2)
test_anti_join1 <- anti_join(results2, results1, by= c(col_b = col_a))
rm(query1)
rm(query2)
rm(results1)
rm(results2)
# Records not in tables ##
View(test_anti_join1)
}}
任何指导或任何其他更好的方法将受到高度赞赏。