sprintf用于rstudio中带有换行符的sql查询

时间:2018-09-22 21:25:56

标签: sql r sql-server string printf

我有以下(非常长的)sql查询:

select distinct m.*
FROM
(
select c.geo as zipcode, 
a.startdate, 
a.data -> 'Tmin' as Tmin,
a.data -> 'Tmax' as Tmax,
a.data -> 'Tavg' -> as Tavg,
a.data -> 'StnPressure' as StnPressure,
a.data -> 'SeaLevel' as SeaLevel ,
ST_DISTANCE(b.geo, c.geo) as distanceS,
RANK() over (partition by c.geo, a.startdate order by ST_DISTANCE(b.geo, c.geo)) as rnkS
from facttable a
inner join shapefile b on (a.shape_id=b.shape_id)
inner join shapefile c on (ST_DWithin(b.geo,c.geo,5000))
where data_id=98765 and 
b.id = '1000' and 
c.id = '2000' and
c.geoid = '12345'
) m
where m.rnkS = 1

我试图将其移植到R并在我的r脚本中保持格式良好,并使用以下语法使用R变量更新查询:

query = sprintf("select distinct m.*
FROM
(
select c.geo as zipcode, 
a.startdate, 
a.data -> 'Tmin' as Tmin,
a.data -> 'Tmax' as Tmax,
a.data -> 'Tavg' -> as Tavg,
a.data -> 'StnPressure' as StnPressure,
a.data -> 'SeaLevel' as SeaLevel ,
ST_DISTANCE(b.geo, c.geo) as distanceS,
RANK() over (partition by c.geo, a.startdate order by ST_DISTANCE(b.geo, c.geo)) as rnkS
from facttable a
inner join shapefile b on (a.shape_id=b.shape_id)
inner join shapefile c on (ST_DWithin(b.geo,c.geo,(%s)))
where data_id=(%s) and 
b.id = (%s) and 
c.id = (%s) and
c.geoid = (%s)
) m
where m.rnkS = 1", var_1, var_2, var_3, var_4, var_5)

但是RStudio中的sprintf函数包括格式化字符和制表符,这样当我查询数据库时,查询永远不会返回。当我手动删除所有的中断和选项卡时,查询有效。

是否可以在我的r脚本中保留此长查询的格式并将其传递给sprintf,而无需删除所有换行符和制表符,以便当我将其发送到数据库时该查询仍然有效?

谢谢

0 个答案:

没有答案