gpt4 book ai didi

sql - 有没有更好的方法在 R 中编写这个 sqlQuery 代码?

转载 作者:行者123 更新时间:2023-12-04 23:33:06 25 4
gpt4 key购买 nike

我正在编写一个 R 脚本来获取一些数据库数据,然后使用 RODBC 包对其进行处理。目前我所有的 sqlQuery 命令都是一个长字符串;

stsample<-sqlQuery(odcon, paste"select * from bob.DESIGNSAMPLE T1, bob.DESIGNSUBJECTGROUP T2, bob.DESIGNEVENT T3, bob.CONFIGSAMPLETYPES T4 WHERE T1.SUBJECTGROUPID = T2.SUBJECTGROUPID AND T1.TREATMENTEVENTID = T3.TREATMENTEVENTID AND T1.SAMPLETYPEKEY = T4.SAMPLETYPEKEY AND T1.STUDYID = T2.STUDYID AND T1.STUDYID = T3.STUDYID AND T1.STUDYID = ", chstudid, sep=""))
head(stsample)

看起来很丑,很难阅读/更新。我试过把它们放在多行中,但是新行字符会妨碍到,目前我最好的是使用大量粘贴;
stsample<-sqlQuery(odcon,
paste(
"select ",
"* ",
"from ",
"BOB.DESIGNSAMPLE T1, ",
"BOB.DESIGNSUBJECTGROUP T2, ",
"BOB.DESIGNEVENT T3, ",
"BOB.CONFIGSAMPLETYPES T4 ",
"WHERE ",
"T1.SUBJECTGROUPID = T2.SUBJECTGROUPID ",
"AND T1.TREATMENTEVENTID = T3.TREATMENTEVENTID ",
"AND T1.SAMPLETYPEKEY = T4.SAMPLETYPEKEY ",
"AND T1.STUDYID = T2.STUDYID ",
"AND T1.STUDYID = T3.STUDYID ",
"AND T1.STUDYID = ",chstudid,
sep="")
)
head(stsample)

但我不喜欢在每一行周围加上引号,并且让我的空格正确。有没有更好的办法 ?

最佳答案

我会使用这样的东西:

stsample<-sqlQuery(odcon,
paste("
####DATASET CONSTRUCTION QUERY #########
select
*
from
BOB.DESIGNSAMPLE T1,
BOB.DESIGNSUBJECTGROUP T2,
BOB.DESIGNEVENT T3,
BOB.CONFIGSAMPLETYPES T4
WHERE
T1.SUBJECTGROUPID = T2.SUBJECTGROUPID
AND T1.TREATMENTEVENTID = T3.TREATMENTEVENTID
AND T1.SAMPLETYPEKEY = T4.SAMPLETYPEKEY
AND T1.STUDYID = T2.STUDYID
AND T1.STUDYID = T3.STUDYID
AND T1.STUDYID =
###################################
", as.character(chstudid), sep="")
)

关于sql - 有没有更好的方法在 R 中编写这个 sqlQuery 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2333436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com