gpt4 book ai didi

python - 查询在查询浏览器中运行良好,但在 python 代码中运行不佳

转载 作者:行者123 更新时间:2023-12-01 01:32:10 26 4
gpt4 key购买 nike

我有一个 teradata sql 查询,它在 Teradata Studio Express 的查询浏览器中运行良好,但是当我尝试在 python 代码中运行相同的查询时,出现以下错误:

(3706, "[42000] [Teradata][ODBC Teradata Driver]Teradata DatabaseSyntax error: expected something between '(' and '('.")

我的查询:

td_query = """select 
hash_md5((XMLAGG(TRIM(h_age) ORDER BY h_age ASC) (VARCHAR(100))) )hash_age,
hash_md5((XMLAGG(TRIM(h_marks) ORDER BY h_age ASC )(VARCHAR(100)))) hash_marks,
hash_md5((XMLAGG(TRIM(h_id) ORDER BY h_age ASC )(VARCHAR(100)))) hash_id,
hash_md5((XMLAGG(TRIM(h_score) ORDER BY h_age ASC )(VARCHAR(100))))hash_score,
hash_md5((XMLAGG(TRIM(h_name) ORDER BY h_age ASC )(VARCHAR(100))))hash_name,
hash_md5((XMLAGG(TRIM(h_entry) ORDER BY h_entry ASC )(VARCHAR(100))))hash_entry,
SUM(Aggrownum) sum_val,
COUNT(Aggrownum) count_val,
sum_val/count_val row_num
from
(
Select
hash_md5(CAST(age AS VARCHAR(100)))h_age ,
hash_md5(CAST(marks AS VARCHAR(100)))h_marks,
hash_md5(CAST(id AS VARCHAR(100)))h_id,
hash_md5(CAST(score AS VARCHAR(100)))h_score,
hash_md5(CAST(f_name AS VARCHAR(100)))h_name,
hash_md5(CAST(entry AS FORMAT 'yyyy-mm-ddbhh:mi:ss') (char (20)))h_entry,
row_number() over (partition by dob order by age asc) rno,round(rno/500.0) Aggrownum
from tdwm.student_2
) A group by Aggrownum ORDER BY row_num;
"""

并在 python 文件中将其运行为:

df_teradata = pd.read_sql(td_query, connect)            

其中 connecttd.UdaExec 对象,上面的代码对于其他更简单的查询(例如 select * from tdwm.student_2)工作正常。

我还尝试了同一查询的最小化版本,但出现了相同的错误。

这是我使用td_query的代码:

import pandas as pd
import teradata as td

td_host, td_username, td_passwd, td_dbname = '10.20.181.55', 'dbac', 'dbac', 'tdwm'

udaExec = td.UdaExec(appName="test", version="1.0", logConsole=False)
with udaExec.connect(method="odbc",system=td_host, username=td_username, port=1025,
password=td_passwd, driver="Teradata Database ODBC Driver 16.20") as connect:
df_teradata = pd.read_sql(td_query, connect)

最佳答案

我遇到了类似的问题..

您的查询的问题在于它包含 hash_md5 函数,而 teradata python 模块 无法识别该函数,这就是错误的原因。

解决方案:

在该函数之前添加数据库名称(就像您在表名称 tdwm.student_2 之前所做的那样)。所以你的新查询将是:

select 
tdwm.hash_md5((XMLAGG(TRIM(h_age) ORDER BY h_age ASC) (VARCHAR(100))) )hash_age,
tdwm.hash_md5((XMLAGG(TRIM(h_marks) ORDER BY h_age ASC )(VARCHAR(100)))) hash_marks,
tdwm.hash_md5((XMLAGG(TRIM(h_id) ORDER BY h_age ASC )(VARCHAR(100)))) hash_id,
tdwm.hash_md5((XMLAGG(TRIM(h_score) ORDER BY h_age ASC )(VARCHAR(100))))hash_score,
tdwm.hash_md5((XMLAGG(TRIM(h_name) ORDER BY h_age ASC )(VARCHAR(100))))hash_name,
tdwm.hash_md5((XMLAGG(TRIM(h_entry) ORDER BY h_entry ASC )(VARCHAR(100))))hash_entry,
SUM(Aggrownum) sum_val,
COUNT(Aggrownum) count_val,
sum_val/count_val row_num
from
(
Select
tdwm.hash_md5(CAST(age AS VARCHAR(100)))h_age ,
tdwm.hash_md5(CAST(marks AS VARCHAR(100)))h_marks,
tdwm.hash_md5(CAST(id AS VARCHAR(100)))h_id,
tdwm.hash_md5(CAST(score AS VARCHAR(100)))h_score,
tdwm.hash_md5(CAST(f_name AS VARCHAR(100)))h_name,
tdwm.hash_md5(CAST(entry AS FORMAT 'yyyy-mm-ddbhh:mi:ss') (char (20)))h_entry,
row_number() over (partition by dob order by age asc) rno,round(rno/500.0) Aggrownum
from tdwm.student_2
) A group by Aggrownum ORDER BY row_num;

关于python - 查询在查询浏览器中运行良好,但在 python 代码中运行不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52762428/

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