sqldf 在使用子查询时出错
sqldf gives error while using subquery
我正在尝试按工作日获取移动平均线,因为我正在使用 sql 查询。
数据框是
和sqldf代码:
ma_782 = sqldf("SELECT
t1.Id_indicator, t1.Hour,
(
select SUM(t2.Value) / COUNT(t2.Value)
FROM max_value_782 AS t2
WHERE
t1.Hour = t2.Hour and
weekdays.Date(t1.Date) = weekdays.Date(t2.Date)
and DATEDIFF(t1.Date, t2.Date) BETWEEN 1 AND 42
) AS 'MA_by_weekday'
FROM max_value_782 AS t1 ;")
这给出了错误
Error in rsqlite_send_query(conn@ptr, statement) : near "(": syntax error
虽然它从简单的 select 开始工作:
sqldf("select * from max_value_782")
考虑替换 weekdays.
方法,因为此语法假定使用 table 限定符。由于默认情况下 sqldf 使用 SQLite 方言,因此使用 strftime
来比较工作日。此外,单引号用于字符串文字而不是包含 table/field 标识符。 SQLite 可以使用方括号、反引号或双引号,或者 none 如果不使用保留 words/special 字符。
ma_782 = sqldf("SELECT t1.Id_indicator, t1.Hour,
(SELECT AVG(t2.Value)
FROM max_value_782 AS t2
WHERE t1.Hour = t2.Hour
AND strftime('%w', t1.Date) = strftime('%w', t2.Date)
AND (t2.Date - t2.Date) BETWEEN 1 AND 42
) AS MA_by_weekday
FROM max_value_782 AS t1;")
我正在尝试按工作日获取移动平均线,因为我正在使用 sql 查询。 数据框是
和sqldf代码:
ma_782 = sqldf("SELECT
t1.Id_indicator, t1.Hour,
(
select SUM(t2.Value) / COUNT(t2.Value)
FROM max_value_782 AS t2
WHERE
t1.Hour = t2.Hour and
weekdays.Date(t1.Date) = weekdays.Date(t2.Date)
and DATEDIFF(t1.Date, t2.Date) BETWEEN 1 AND 42
) AS 'MA_by_weekday'
FROM max_value_782 AS t1 ;")
这给出了错误
Error in rsqlite_send_query(conn@ptr, statement) : near "(": syntax error
虽然它从简单的 select 开始工作:
sqldf("select * from max_value_782")
考虑替换 weekdays.
方法,因为此语法假定使用 table 限定符。由于默认情况下 sqldf 使用 SQLite 方言,因此使用 strftime
来比较工作日。此外,单引号用于字符串文字而不是包含 table/field 标识符。 SQLite 可以使用方括号、反引号或双引号,或者 none 如果不使用保留 words/special 字符。
ma_782 = sqldf("SELECT t1.Id_indicator, t1.Hour,
(SELECT AVG(t2.Value)
FROM max_value_782 AS t2
WHERE t1.Hour = t2.Hour
AND strftime('%w', t1.Date) = strftime('%w', t2.Date)
AND (t2.Date - t2.Date) BETWEEN 1 AND 42
) AS MA_by_weekday
FROM max_value_782 AS t1;")