for循环仅检查向量中的最后一个值以使用R检查SQL Server中的重复项?
Posted
技术标签:
【中文标题】for循环仅检查向量中的最后一个值以使用R检查SQL Server中的重复项?【英文标题】:for Loop only check the last value in the vector to check duplicates in SQL Server with R? 【发布时间】:2021-12-30 03:24:43 【问题描述】:library(odbc)
library(DBI)
library(pool)
myDatabase <- 'MyDB'
myDriver <- 'SQL Server'
myServer <- 'localhost\\SQLEXPRESS'
con <- pool::dbPool(odbc::odbc(), Driver = myDriver,
Server = myServer,
Database = myDatabase,
Trusted_Connection = 'True')
Caddy_id <- c("C1","C2","C3","C4","C5","C6","C7","C8","C9","C10")
for(i in 1:length(Caddy_id))
check_duplicate <- dbGetQuery(con,paste0("DECLARE @container AS INT = 0
IF EXISTS(SELECT 1 FROM dbo.P1000_Packing WITH(NOLOCK)
WHERE Caddy_1 = '",Caddy_id[i],"' OR
Caddy_2 = '",Caddy_id[i],"' OR
Caddy_3 = '",Caddy_id[i],"' OR
Caddy_4 = '",Caddy_id[i],"' OR
Caddy_5 = '",Caddy_id[i],"' OR
Caddy_6 = '",Caddy_id[i],"' OR
Caddy_7 = '",Caddy_id[i],"' OR
Caddy_8 = '",Caddy_id[i],"' OR
Caddy_9 = '",Caddy_id[i],"' OR
Caddy_10 = '",Caddy_id[i],"')
BEGIN
SET @container = 1
END
SELECT @container;"))
check_duplicate
我想用 R 检查 SQL Server 中所有重复值,但只能检查向量 Caddy_id 中的最后一个值。
为什么只有值10返回1,而另外一个值即使存在于sql表中也会返回0?
此表返回 1
.
但是这个表返回 0
如果我改变矢量Caddy_id <- c("C0","C1","C2","C3","C4","C5","C6","C7","C8","C9")
它还返回 1
【问题讨论】:
因为你每次循环都在替换check_duplicate
变量的值,所以循环退出时只对应Caddy_id[I]
的最后一个值,也就是@987654329 @。一种选择是使用另一个名为(比如)result
的变量,在循环之前将其设置为 0,如果check_duplicate
在循环内为 1,则将其设置为 1。还有其他选项(例如使用 SQL in
,或者读取您的表并在 R 中查找向量变量),但这看起来对您的代码的更改最少
【参考方案1】:
目前,您的查询在每次迭代中自行运行,其中 @container
每次都重置为零。因此,只会保留最后一次运行的计数,并且每次都会替换 check_duplicate
。
但是,考虑通过为每个球童 ID 构建一个聚合计数向量列表来计算 R 中的重复项。确保还使用参数安全地运行查询,其中一种方法包括 sqlInterpolate
。
Caddy_id <- c("C1","C2","C3","C4","C5","C6","C7","C8","C9","C10")
sql = "SELECT COUNT(*) AS [count]
FROM dbo.P1000_Packing
WHERE Caddy_1 = ?cad_id OR
Caddy_2 = ?cad_id OR
Caddy_3 = ?cad_id OR
Caddy_4 = ?cad_id OR
Caddy_5 = ?cad_id OR
Caddy_6 = ?cad_id OR
Caddy_7 = ?cad_id OR
Caddy_8 = ?cad_id OR
Caddy_9 = ?cad_id OR
Caddy_10 = ?cad_id"
check_duplicates <- sapply(
Caddy_id,
function(i)
qry <- sqlInterpolate(con, sql, ?cad_id = i)
dbGetQuery(con, qry)$count
)
total_duplicates <- sum(check_duplicates, na.rm = TRUE)
另一种 SQL 查询:
SELECT COUNT(*) AS [count]
FROM dbo.P1000_Packing
WHERE ?cad_id IN (
Caddy_1, Caddy_2, Caddy_3, Caddy_4, Caddy_5,
Caddy_6, Caddy_7, Caddy_8, Caddy_9, Caddy_10
)
【讨论】:
@Taufiq 如果这回答了你的问题,那么请“接受”它。以上是关于for循环仅检查向量中的最后一个值以使用R检查SQL Server中的重复项?的主要内容,如果未能解决你的问题,请参考以下文章
Vue - 检查你是不是在 v-for 循环的最后一个道具上