python返回一个空数据框

时间:2015-08-20 01:27:19

标签: python sql pandas dataframe

特别是python和pandas有点新鲜。我尝试运行查询(通过我公司的数据库连接)来获取信息,使用store_id设置参数,然后输出到csv。我知道这个sql返回数据,但由于某种原因,它返回一个空数据帧。

import sys
import os
import pandas as pd
import MySQLdb
import pandas.io.sql as psql
from datetime import datetime
from sql_connect import connect_to_sql, connect_to_analytics #imports credentials and connects you to DB

con = connect_to_analytics()    

print "store id"
store = raw_input() 

#pull sales metrics by rule

sql = """SELECT day_date, rule.name, analytics.a.sku, pca.att_value as 'Category', sum(analytics.a.revenue) as 'revenue', sum(analytics.a.sales) as 'quantity', MAX(analytics.a.price) as 'price', sum(analytics.a.profit) as 'profit', SUM(analytics.a.profit)/SUM(analytics.a.revenue) AS 'margin', sum(analytics.a.hits) as 'total hits', sum(pps.in_stock) as 'inventory', sum(analytics.a.sales)/(sum(pps.in_stock) + sum(analytics.a.sales)) as 'sell thru'
FROM analytics.analytics_daily_skus_data as a
JOIN wp_data_prod.products_per_store as pps on analytics.a.store_id = pps.store_id AND analytics.a.sku = pps.sku
JOIN wp_data_prod.user_rule as rule ON pps.category_id = rule.id
JOIN wp_data_prod.pps_custom_attributes as pca on pps.id = pca.pps_id
WHERE pps.store_id =%s
AND analytics.a.day_date BETWEEN '2015-07-01'AND '2015-07-30'
AND pca.att_name = "str_category"
GROUP BY analytics.a.sku, day_date
ORDER BY analytics.a.sku, day_date""" 

df = pd.read_sql(sql % store, con=con)
print df

这是输出:

In [33]: print df
Empty DataFrame
Columns: [day_date, name, sku, Category, revenue, quantity, price, profit, margin, total hits, inventory, sell thru]
Index: []

解决方案(今天想出来):

sql = """SELECT day_date, rule.name, analytics.a.sku, pca.att_value as 'Category', sum(analytics.a.revenue) as 'revenue', sum(analytics.a.sales) as 'quantity', MAX(analytics.a.price) as 'price', sum(analytics.a.profit) as 'profit', SUM(analytics.a.profit)/SUM(analytics.a.revenue) AS 'margin', sum(analytics.a.hits) as 'total hits', sum(pps.in_stock) as 'inventory', sum(analytics.a.sales)/(sum(pps.in_stock) + sum(analytics.a.sales)) as 'sell thru'
FROM analytics.analytics_daily_skus_data as a
JOIN wp_data_prod.products_per_store as pps on analytics.a.store_id = pps.store_id AND analytics.a.sku = pps.sku
JOIN wp_data_prod.user_rule as rule ON pps.category_id = rule.id
JOIN wp_data_prod.pps_custom_attributes as pca on pps.id = pca.pps_id
WHERE pps.store_id =('%s')
AND analytics.a.day_date BETWEEN '2015-07-01'AND '2015-07-30'
AND pca.att_name = "str_category"
GROUP BY analytics.a.sku, day_date
ORDER BY analytics.a.sku, day_date""" % (store)

0 个答案:

没有答案