循环运行单个sql查询并将输出保存在一个数据框中

时间:2019-04-03 05:46:25

标签: python sql pandas pandas-groupby

我需要循环运行此单个sql查询,并将输出保存在单个dataframe中。我写了这段代码,但只有DEF数据作为输出。

import pyodbc
import pandas as pd


cust =['ABC','DEF']
for i in cust:
    try:
        cnxn = pyodbc.connect(driver='{SQL Server}', host='172.29.31.23,63678', database=      i,trusted_connection='yes', user='', password='')

    except:
          print('error')

          sql_query="SELECT  Customer,[Type],count(*)  FROM dbo.data WHERE DATEADD(mi, 270, [ Date]) BETWEEN '2019-01-01 00:00:00' AND '2019-02-01 00:00:00' GROUP BY Customer, [Type]ORDER BY count(*) DESC";


          for chunk in pd.read_sql_query(sql_query , chunksize=100):


             cursor = cnxn.cursor()


             df_Jan2 = pd.read_sql(sql_query, cnxn)

0 个答案:

没有答案