SQL 比较运算符问题)...在第 1 行的 '>'2021-06-01' AND 100*(d1_close-close)/close>'29'' 附近使用正确的语法")
SQL Comparison Operator Issue) ...right syntax to use near '>'2021-06-01' AND 100*(d1_close-close)/close>'29'' at line 1")
我正在尝试 运行 通过 python 文件进行 SQL 查询,但收到错误消息
... right syntax to use near '>'2021-06-01' AND 100*(d1_close-close)/close>'29'' at line 1")
我的查询代码如下:
sql = "WITH comparing_price AS (SELECT CODE, DATE, OPEN, high, low, close, volume,"\
"LEAD(OPEN, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_open',"\
"LEAD(OPEN, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_open',"\
"LEAD(OPEN, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_open',"\
"LEAD(high, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_high',"\
"LEAD(high, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_high',"\
"LEAD(high, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_high',"\
"LEAD(high, 4) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd4_high',"\
"LEAD(low, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_low',"\
"LEAD(low, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_low',"\
"LEAD(low, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_low',"\
"LEAD(close, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_close',"\
"LEAD(close, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_close',"\
"LEAD(close, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_close',"\
"LEAD(volume, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_volume',"\
"LEAD(volume, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_volume',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 2 preceding AND 2 following) AS 'd2_MA5',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 7 preceding AND 2 following) AS 'd2_MA10',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_MA20',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 57 preceding AND 2 following) AS 'd2_MA60',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 117 preceding AND 2 following) AS 'd2_MA120',"\
"STD(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_std'"\
"FROM daily_price)"\
"SELECT * "\
"FROM comparing_price"\
"WHERE DATE>\'2021-06-01\' AND 100*(d1_close-close)/close>\'29\'"
curs.execute(sql)
我也尝试过如下不同的版本:
conditions=('2021-06-01','29')
...
....
"WHERE DATE>%s AND 100*(d1_close-close)/close>%s"
curs.execute(sql, conditions)
UPDATE)) 我认为语法错误出在比较运算符中。如何将比较运算符从 python 发送到 SQL?
三重引号允许您在不使用字符串连接的情况下编写查询,因为字符串可以跨越多行:
sql = """WITH comparing_price AS (SELECT CODE, DATE, OPEN, high, low, close, volume,
LEAD(OPEN, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_open',
LEAD(OPEN, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_open',
LEAD(OPEN, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_open',
"LEAD(high, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_high',
LEAD(high, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_high',
LEAD(high, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_high',
LEAD(high, 4) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd4_high',
LEAD(low, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_low',
LEAD(low, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_low',
LEAD(low, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_low',
LEAD(close, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_close',
LEAD(close, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_close',"\
LEAD(close, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_close',
LEAD(volume, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_volume',
LEAD(volume, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_volume',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 2 preceding AND 2 following) AS 'd2_MA5',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 7 preceding AND 2 following) AS 'd2_MA10',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_MA20',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 57 preceding AND 2 following) AS 'd2_MA60',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 117 preceding AND 2 following) AS 'd2_MA120',
STD(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_std'
FROM daily_price)
SELECT *
FROM comparing_price
WHERE DATE > '2021-06-01' AND 100*(d1_close-close)/close"""
这也使得测试更容易,因为您只需将查询复制到另一个工具即可 运行 它。
我正在尝试 运行 通过 python 文件进行 SQL 查询,但收到错误消息
... right syntax to use near '>'2021-06-01' AND 100*(d1_close-close)/close>'29'' at line 1")
我的查询代码如下:
sql = "WITH comparing_price AS (SELECT CODE, DATE, OPEN, high, low, close, volume,"\
"LEAD(OPEN, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_open',"\
"LEAD(OPEN, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_open',"\
"LEAD(OPEN, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_open',"\
"LEAD(high, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_high',"\
"LEAD(high, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_high',"\
"LEAD(high, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_high',"\
"LEAD(high, 4) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd4_high',"\
"LEAD(low, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_low',"\
"LEAD(low, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_low',"\
"LEAD(low, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_low',"\
"LEAD(close, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_close',"\
"LEAD(close, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_close',"\
"LEAD(close, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_close',"\
"LEAD(volume, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_volume',"\
"LEAD(volume, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_volume',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 2 preceding AND 2 following) AS 'd2_MA5',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 7 preceding AND 2 following) AS 'd2_MA10',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_MA20',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 57 preceding AND 2 following) AS 'd2_MA60',"\
"AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 117 preceding AND 2 following) AS 'd2_MA120',"\
"STD(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_std'"\
"FROM daily_price)"\
"SELECT * "\
"FROM comparing_price"\
"WHERE DATE>\'2021-06-01\' AND 100*(d1_close-close)/close>\'29\'"
curs.execute(sql)
我也尝试过如下不同的版本:
conditions=('2021-06-01','29')
...
....
"WHERE DATE>%s AND 100*(d1_close-close)/close>%s"
curs.execute(sql, conditions)
UPDATE)) 我认为语法错误出在比较运算符中。如何将比较运算符从 python 发送到 SQL?
三重引号允许您在不使用字符串连接的情况下编写查询,因为字符串可以跨越多行:
sql = """WITH comparing_price AS (SELECT CODE, DATE, OPEN, high, low, close, volume,
LEAD(OPEN, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_open',
LEAD(OPEN, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_open',
LEAD(OPEN, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_open',
"LEAD(high, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_high',
LEAD(high, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_high',
LEAD(high, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_high',
LEAD(high, 4) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd4_high',
LEAD(low, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_low',
LEAD(low, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_low',
LEAD(low, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_low',
LEAD(close, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_close',
LEAD(close, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_close',"\
LEAD(close, 3) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd3_close',
LEAD(volume, 1) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd1_volume',
LEAD(volume, 2) OVER (PARTITION BY CODE ORDER BY DATE) AS 'd2_volume',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 2 preceding AND 2 following) AS 'd2_MA5',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 7 preceding AND 2 following) AS 'd2_MA10',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_MA20',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 57 preceding AND 2 following) AS 'd2_MA60',
AVG(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 117 preceding AND 2 following) AS 'd2_MA120',
STD(close) OVER (PARTITION BY CODE ORDER BY DATE, DATE ROWS BETWEEN 17 preceding AND 2 following) AS 'd2_std'
FROM daily_price)
SELECT *
FROM comparing_price
WHERE DATE > '2021-06-01' AND 100*(d1_close-close)/close"""
这也使得测试更容易,因为您只需将查询复制到另一个工具即可 运行 它。