我是Python初学者。我发现这段代码非常适合我(将IP21连接到Power BI)-只是试图增加标签数量(信息)
import pandas as pd
import pyodbc
conn = pyodbc.connect('NAME')
tag = 'MYTAG'
start = '2019-01-01 12:00:00'
end = '2019-10-02 12:00:00'
sql = "select TS,VALUE from HISTORY "\
"where NAME='%s'"\
"and PERIOD = 60*10"\
"and REQUEST = 2"\
"and REQUEST=2 and TS between TIMESTAMP'%s' and TIMESTAMP'%s'" % (tag, start, end)
data = pd.read_sql(sql,conn) # Pandas DataFrame with your data!
相反,我只需要tag1,tag2,...并获取包含日期和所有这些值的表数据。如何更改代码?我得到一个使代码运行的答案,但是我认为我的续集编程搞砸了。名称不是一个标签,而是使搜索名称(tag1,tag2)无效。您如何在续集中找到多个东西? 我想将其更改为
tag = ' TAG1'
tag2 =' TAG2'
start = '2019-01-01 12:00:00'
end = '2019-10-02 12:00:00'
sql = "select TS,VALUE from HISTORY "\
"where NAME=('%s,%s)'"\
"and PERIOD = 60*10"\
"and REQUEST = 2"\
"and REQUEST=2 and TS between TIMESTAMP'%s' and TIMESTAMP'%s'" % (tag, tag2, start, end)
data = pd.read_sql(sql,conn) # Pandas DataFrame with your data!
类似的东西。 这有效,但提供了一个空表。谢谢!
答案 0 :(得分:1)
如果我理解得很好。您想要一些标签的信息。因此,只需调整查询中标签的条件即可。您应该注意的是,每个标签都有一个%s。因此,如果要添加标签,请在查询中添加%s,并在括号中的%之后添加相应的变量。尝试学习用python替换字符串的人。很简单。
import pandas as pd
import pyodbc
conn = pyodbc.connect('NAME')
tag = 'MYTAG'
start = '2019-01-01 12:00:00'
end = '2019-10-02 12:00:00'
sql = "select TS,VALUE from HISTORY "\
"WHERE NAME in ('%s', '%s')"\
"and PERIOD = 60*10"\
"and REQUEST = 2"\
"and REQUEST=2 and TS between TIMESTAMP'%s' and TIMESTAMP'%s'" % (tag, start, end)
data = pd.read_sql(sql,conn) # Pandas DataFrame with your data!