因此,我创建了一个动态数据透视查询,该查询在SQL Server Management Studio中非常有效。
动态数据透视查询代码:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT ',' + QUOTENAME(PatternID)
from loglist
group by PatternID
order by PatternID
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT FileName,' + @cols + ' from
(
select FileName, PatternID, Found
from loglist
) x
pivot
(
sum(Found)
for PatternID in (' + @cols + ')
) p '
execute(@query);
输出:
f 1 1 1 NULL NULL NULL
q 1 1 1 NULL NULL NULL
e 1 1 1 NULL NULL NULL
s NULL NULL NULL 0 0 0
现在,我想将此数据存储到Pandas数据框中,并且也自动存储为.csv
文件。
所以
def analytics():
db = pyodbc.connect(driver="**",
host="**",
database="**",
trusted_connection="**",
user="**",
password="**",
port="**")
cur = db.cursor()
textfile = open('analytics.script', 'r')
filetext = textfile.read()
textfile.close()
print(filetext)
cur.execute(SqlCommand)
# store it in pandas Data Frame here.
analytics()
但是它显示此错误:
DataError: ('22018', "[22018] [Microsoft][SQL ***][SQL Server]Conversion failed when converting the nvarchar value 'long file name here' to data type int. (245) (SQLExecDirectW)")
analytics.script
包含动态数据透视查询。
在这里做什么?
预先感谢