使用单引号和双引号的混合创建粘合字符串
Create glue string with mixture of single and double quotes
我正在尝试使用 R
中的 glue
包创建一个字符串
这是 'single' 和 "double" 引号的混合。
作为代表,考虑以下类型的 SQL
查询
我要构建的字符串:
CREATE TABLE fact_final_table AS
(SELECT tab1.id,
AVG(tab2."MV") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '7'
AND tab1.start_date::date - integer '1') AS "mv_avg_1w",
AVG(tab2."MV") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '14'
AND tab1.start_date::date - integer '1') AS "mv_avg_2w"
FROM (SELECT id,
start_date,
point
FROM base_tab
WHERE mpfb.start_date::date >= '01-01-2000'::date) AS tab1
LEFT JOIN ghcnd_observations AS tab2
ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '180')
AND (tab1.start_date::date - integer '1')
AND ST_DWithin(tab1.point, tab2.location, 0.5))
GROUP BY tab1.id);
如你所见,它是单引号和双引号的组合,
正如上面所写的那样,这些字面意思是很重要的。为了
例如 tab2."MV"
有双引号,而 tab1.start_date::date - integer '7' AND tab1.start_date::date - integer '1'
有单引号
需要按字面意思保留的引用。
这个字符串也需要使用参数来构建。
我在 R
中使用 glue
尝试了以下操作,但无法获取
上班。
var1 <- "MV"
var1_lowcase <- "mv"
lag_days <- 180
var_date <- as.Date("2000-01-01")
var_dwithin <- 0.5
glue::glue(
"CREATE TABLE fact_final_table AS
(SELECT tab1.id,
AVG(tab2."{var1}") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '7'
AND tab1.start_date::date - integer '1') AS "{var1_lowcase}_avg_1w",
AVG(tab2."{var1}") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '14'
AND tab1.start_date::date - integer '1') AS "{var1_lowcase}_avg_2w"
FROM (SELECT id,
start_date,
point
FROM base_tab
WHERE mpfb.start_date::date >= '{format(var_date, "%d-%m-%Y")}'::date) AS tab1
LEFT JOIN ghcnd_observations AS tab2
ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '{lag_days}')
AND (tab1.start_date::date - integer '1')
AND ST_DWithin(tab1.point, tab2.location, {var_dwithin}))
GROUP BY tab1.id);")
不幸的是,由于 single/double 引号不存在,这不起作用
在 glue::glue(...)
.
中正确转义
任何人都可以在这里提供最少的帮助吗
打破所需的输出字符串?我不确定这是否容易
可行的。我将不胜感激任何其他 tidy
方法,例如例如,也使用 stringr
,因为我希望它 %>%
友好。
我简单地看了 glue::glue_sql
但不知道如何直接
在这里应用它。如果能在此处了解如何使用它,我将不胜感激。
尝试转义双引号:
glue::glue(
"CREATE TABLE fact_final_table AS
(SELECT tab1.id,
AVG(tab2.\"{var1}\") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '7'
AND tab1.start_date::date - integer '1') AS \"{var1_lowcase}_avg_1w\",
AVG(tab2.\"{var1}\") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '14'
AND tab1.start_date::date - integer '1') AS \"{var1_lowcase}_avg_2w\"
FROM (SELECT id,
start_date,
point
FROM base_tab
WHERE mpfb.start_date::date >= '{format(var_date, \"%d-%m-%Y\")}'::date)
AS tab1
LEFT JOIN ghcnd_observations AS tab2
ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '{lag_days}')
AND (tab1.start_date::date - integer '1')
AND ST_DWithin(tab1.point, tab2.location, {var_dwithin}))
GROUP BY tab1.id);")
其中 returns :
#CREATE TABLE fact_final_table AS
#(SELECT tab1.id,
#AVG(tab2."MV") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date #- integer '7'
#AND tab1.start_date::date - integer '1') AS "mv_avg_1w",
#AVG(tab2."MV") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date #- integer '14'
#AND tab1.start_date::date - integer '1') AS "mv_avg_2w"
#FROM (SELECT id,
#start_date,
#point
#FROM base_tab
#WHERE mpfb.start_date::date >= '01-01-2000'::date) AS tab1
#LEFT JOIN ghcnd_observations AS tab2
#ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '180')
#AND (tab1.start_date::date - integer '1')
#AND ST_DWithin(tab1.point, tab2.location, 0.5))
#GROUP BY tab1.id);
所以我从昨天开始更详细地研究了这个问题,并且
看起来 glue
确实具有 单身的功能
和双引号显式 即 glue::single_quote()
和
glue::double_quote()
.
与@ronakshah 的(有帮助的)回复类似,我设法
以下,更明确(为了代码可读性):
var1 <- "MV"
var1_lowcase <- "mv"
lag_days <- 180
var_date <- as.Date("2000-01-01")
var_dwithin <- 0.5
glue::glue(
"CREATE TABLE fact_final_table AS
(SELECT tab1.id,
AVG(tab2.{glue::double_quote(var1)}) FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '7'
AND tab1.start_date::date - integer '1') AS {glue::double_quote(glue::glue({var1_lowcase},'_avg_1w'))},
AVG(tab2.{glue::double_quote(var1)}) FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '14'
AND tab1.start_date::date - integer '1') AS {glue::double_quote(glue::glue({var1_lowcase},'_avg_2w'))}
FROM (SELECT id,
start_date,
point
FROM base_tab
WHERE mpfb.start_date::date >= {glue::single_quote(format(var_date, '%d-%m-%Y'))}::date)
AS tab1
LEFT JOIN ghcnd_observations AS tab2
ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '{lag_days}')
AND (tab1.start_date::date - integer '1')
AND ST_DWithin(tab1.point, tab2.location, {var_dwithin}))
GROUP BY tab1.id);")
其中 returns 我需要的主要字符串。希望这可以帮助别人
谁有类似的 glue
要求。
我正在尝试使用 R
中的 glue
包创建一个字符串
这是 'single' 和 "double" 引号的混合。
作为代表,考虑以下类型的 SQL
查询
我要构建的字符串:
CREATE TABLE fact_final_table AS
(SELECT tab1.id,
AVG(tab2."MV") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '7'
AND tab1.start_date::date - integer '1') AS "mv_avg_1w",
AVG(tab2."MV") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '14'
AND tab1.start_date::date - integer '1') AS "mv_avg_2w"
FROM (SELECT id,
start_date,
point
FROM base_tab
WHERE mpfb.start_date::date >= '01-01-2000'::date) AS tab1
LEFT JOIN ghcnd_observations AS tab2
ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '180')
AND (tab1.start_date::date - integer '1')
AND ST_DWithin(tab1.point, tab2.location, 0.5))
GROUP BY tab1.id);
如你所见,它是单引号和双引号的组合,
正如上面所写的那样,这些字面意思是很重要的。为了
例如 tab2."MV"
有双引号,而 tab1.start_date::date - integer '7' AND tab1.start_date::date - integer '1'
有单引号
需要按字面意思保留的引用。
这个字符串也需要使用参数来构建。
我在 R
中使用 glue
尝试了以下操作,但无法获取
上班。
var1 <- "MV"
var1_lowcase <- "mv"
lag_days <- 180
var_date <- as.Date("2000-01-01")
var_dwithin <- 0.5
glue::glue(
"CREATE TABLE fact_final_table AS
(SELECT tab1.id,
AVG(tab2."{var1}") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '7'
AND tab1.start_date::date - integer '1') AS "{var1_lowcase}_avg_1w",
AVG(tab2."{var1}") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '14'
AND tab1.start_date::date - integer '1') AS "{var1_lowcase}_avg_2w"
FROM (SELECT id,
start_date,
point
FROM base_tab
WHERE mpfb.start_date::date >= '{format(var_date, "%d-%m-%Y")}'::date) AS tab1
LEFT JOIN ghcnd_observations AS tab2
ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '{lag_days}')
AND (tab1.start_date::date - integer '1')
AND ST_DWithin(tab1.point, tab2.location, {var_dwithin}))
GROUP BY tab1.id);")
不幸的是,由于 single/double 引号不存在,这不起作用
在 glue::glue(...)
.
任何人都可以在这里提供最少的帮助吗
打破所需的输出字符串?我不确定这是否容易
可行的。我将不胜感激任何其他 tidy
方法,例如例如,也使用 stringr
,因为我希望它 %>%
友好。
我简单地看了 glue::glue_sql
但不知道如何直接
在这里应用它。如果能在此处了解如何使用它,我将不胜感激。
尝试转义双引号:
glue::glue(
"CREATE TABLE fact_final_table AS
(SELECT tab1.id,
AVG(tab2.\"{var1}\") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '7'
AND tab1.start_date::date - integer '1') AS \"{var1_lowcase}_avg_1w\",
AVG(tab2.\"{var1}\") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '14'
AND tab1.start_date::date - integer '1') AS \"{var1_lowcase}_avg_2w\"
FROM (SELECT id,
start_date,
point
FROM base_tab
WHERE mpfb.start_date::date >= '{format(var_date, \"%d-%m-%Y\")}'::date)
AS tab1
LEFT JOIN ghcnd_observations AS tab2
ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '{lag_days}')
AND (tab1.start_date::date - integer '1')
AND ST_DWithin(tab1.point, tab2.location, {var_dwithin}))
GROUP BY tab1.id);")
其中 returns :
#CREATE TABLE fact_final_table AS
#(SELECT tab1.id,
#AVG(tab2."MV") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date #- integer '7'
#AND tab1.start_date::date - integer '1') AS "mv_avg_1w",
#AVG(tab2."MV") FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date #- integer '14'
#AND tab1.start_date::date - integer '1') AS "mv_avg_2w"
#FROM (SELECT id,
#start_date,
#point
#FROM base_tab
#WHERE mpfb.start_date::date >= '01-01-2000'::date) AS tab1
#LEFT JOIN ghcnd_observations AS tab2
#ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '180')
#AND (tab1.start_date::date - integer '1')
#AND ST_DWithin(tab1.point, tab2.location, 0.5))
#GROUP BY tab1.id);
所以我从昨天开始更详细地研究了这个问题,并且
看起来 glue
确实具有 单身的功能
和双引号显式 即 glue::single_quote()
和
glue::double_quote()
.
与@ronakshah 的(有帮助的)回复类似,我设法 以下,更明确(为了代码可读性):
var1 <- "MV"
var1_lowcase <- "mv"
lag_days <- 180
var_date <- as.Date("2000-01-01")
var_dwithin <- 0.5
glue::glue(
"CREATE TABLE fact_final_table AS
(SELECT tab1.id,
AVG(tab2.{glue::double_quote(var1)}) FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '7'
AND tab1.start_date::date - integer '1') AS {glue::double_quote(glue::glue({var1_lowcase},'_avg_1w'))},
AVG(tab2.{glue::double_quote(var1)}) FILTER (WHERE tab2.record_dt BETWEEN tab1.start_date::date - integer '14'
AND tab1.start_date::date - integer '1') AS {glue::double_quote(glue::glue({var1_lowcase},'_avg_2w'))}
FROM (SELECT id,
start_date,
point
FROM base_tab
WHERE mpfb.start_date::date >= {glue::single_quote(format(var_date, '%d-%m-%Y'))}::date)
AS tab1
LEFT JOIN ghcnd_observations AS tab2
ON (tab2.record_dt BETWEEN (tab1.start_date::date - integer '{lag_days}')
AND (tab1.start_date::date - integer '1')
AND ST_DWithin(tab1.point, tab2.location, {var_dwithin}))
GROUP BY tab1.id);")
其中 returns 我需要的主要字符串。希望这可以帮助别人
谁有类似的 glue
要求。