Python 查询返回昨天的结果';s数据库(它与X文件类似,但用于数据)

Python 查询返回昨天的结果';s数据库(它与X文件类似,但用于数据),python,sql-server,pyodbc,Python,Sql Server,Pyodbc,我在Python中看到了一些东西,这绝对让我大吃一惊 运行以下脚本时,我得到的结果与数据库中的结果不匹配: import pyodbc conn = pyodbc.connect('DSN=example;Description=example;UID=example;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=example;Database = example') cursor = conn.cursor() QS = ""

我在Python中看到了一些东西,这绝对让我大吃一惊

运行以下脚本时,我得到的结果与数据库中的结果不匹配:

import pyodbc

conn = pyodbc.connect('DSN=example;Description=example;UID=example;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=example;Database = example')
cursor = conn.cursor()

QS = """select
                    cct.product_group,
                    cct.website,
                    SUM(cct.sales_value) as sales,
                    SUM(cct.sales_value) - SUM(cct.cogs) +  SUM(cct.carriage_reclaim) - SUM(cct.carriage_costs) - SUM(cct.carriage_costs_estimate) - SUM(cct.metapack_costs) - SUM(cct.mktg_costs) - SUM(cct.mktg_costs_estimate) - SUM(cct.finance_costs) - SUM(cct.royalty) - SUM(cct.scrap_costs) as cavo,
                    sum(cct.budget_sales_value) as budget_sales,
                    sum(cct.budget_sales_phased) as budget_sales_phased,
                    sum(budget_sales_value - budget_cogs + budget_carriage_reclaim - budget_carriage_costs - budget_metapack_costs - budget_mktg_costs - budget_affiliate_costs - budget_offline_costs - budget_royalty - budget_finance_costs - budget_scrap_costs) as budget_cavo
                from
                        Phocas_TG.dbo.cavo_cube_trans cct
                    where
                        cct.date between '2016-02-01' and '2016-02-01'
                    and
                        cct.product_group in ('BATH', 'HEAT', 'SOLA')
                group by
                    cct.product_group,
                    cct.website
                """

cursor.execute(QS)

row = 1

while row:
    row = cursor.fetchone()
    print row
结果(错误):

但是,如果我运行相同的脚本,但稍微更改sql字符串(在其中一行中添加一点空格),我会得到正确的结果:

import pyodbc

conn = pyodbc.connect('DSN=example;Description=example;UID=example;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=example;Database = example')
cursor = conn.cursor()

QS = """select
                     cct.product_group,
                    cct.website,
                    SUM(cct.sales_value) as sales,
                    SUM(cct.sales_value) - SUM(cct.cogs) +  SUM(cct.carriage_reclaim) - SUM(cct.carriage_costs) - SUM(cct.carriage_costs_estimate) - SUM(cct.metapack_costs) - SUM(cct.mktg_costs) - SUM(cct.mktg_costs_estimate) - SUM(cct.finance_costs) - SUM(cct.royalty) - SUM(cct.scrap_costs) as cavo,
                    sum(cct.budget_sales_value) as budget_sales,
                    sum(cct.budget_sales_phased) as budget_sales_phased,
                    sum(budget_sales_value - budget_cogs + budget_carriage_reclaim - budget_carriage_costs - budget_metapack_costs - budget_mktg_costs - budget_affiliate_costs - budget_offline_costs - budget_royalty - budget_finance_costs - budget_scrap_costs) as budget_cavo
                from
                        Phocas_TG.dbo.cavo_cube_trans cct
                    where
                        cct.date between '2016-02-01' and '2016-02-01'
                    and
                        cct.product_group in ('BATH', 'HEAT', 'SOLA')
                group by
                    cct.product_group,
                    cct.website
                """

cursor.execute(QS)

row = 1

while row:
    row = cursor.fetchone()
    print row
结果(正确):

如果有帮助的话,数据库本质上是一个每天晚上重建的数据立方体。第一个查询的结果似乎与昨天的数据库相匹配——这可能是某种缓存系统吗?已运行查询的结果存储在何处


我太糊涂了!有人见过这个吗?

嗯?老实说,我不明白这个问题。为什么不将其作为存储过程而不是传递sql?还有,既然两个日期完全相同,为什么还要在两个日期之间费事呢?除了2016-02-01之外,没有其他价值在那里有效。一个简单的等式会更有意义。Hi@SeanLange,问题更多的是理解发生这种情况的原因,而不是评估我的代码。在我运行的生产脚本中,这两个日期是可变的,通过字符串格式传递。然后使用结果生成报告,并通过电子邮件发送。我得到了意想不到的结果,我隔离了问题并将其发布在这里。我有一种感觉,它们是引起sql注入问题的变量。我们面临的挑战是,如果不付出相当大的努力,我们无法看到您的表或执行此操作。所描述的内容听起来不太可能,但很明显,如果你能重新创建它,就会发生一些事情。事实上,我每次运行第一个查询时都会得到相同的错误结果,每次运行第二个查询时都会得到相同的正确结果。唯一的区别是我在第二个查询的第二行开始处添加了一个空格。这一定是因为以前查询的结果存储在某种缓存中,但不幸的是,我对此知之甚少。查询结果不会存储在缓存中。执行计划存储在缓存中,但不存储实际结果。为什么不把它扔进一个程序?它将在可视层和数据层之间实现分离。此外,我怀疑当您开始使用过程时,这个问题会自行解决。
import pyodbc

conn = pyodbc.connect('DSN=example;Description=example;UID=example;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=example;Database = example')
cursor = conn.cursor()

QS = """select
                     cct.product_group,
                    cct.website,
                    SUM(cct.sales_value) as sales,
                    SUM(cct.sales_value) - SUM(cct.cogs) +  SUM(cct.carriage_reclaim) - SUM(cct.carriage_costs) - SUM(cct.carriage_costs_estimate) - SUM(cct.metapack_costs) - SUM(cct.mktg_costs) - SUM(cct.mktg_costs_estimate) - SUM(cct.finance_costs) - SUM(cct.royalty) - SUM(cct.scrap_costs) as cavo,
                    sum(cct.budget_sales_value) as budget_sales,
                    sum(cct.budget_sales_phased) as budget_sales_phased,
                    sum(budget_sales_value - budget_cogs + budget_carriage_reclaim - budget_carriage_costs - budget_metapack_costs - budget_mktg_costs - budget_affiliate_costs - budget_offline_costs - budget_royalty - budget_finance_costs - budget_scrap_costs) as budget_cavo
                from
                        Phocas_TG.dbo.cavo_cube_trans cct
                    where
                        cct.date between '2016-02-01' and '2016-02-01'
                    and
                        cct.product_group in ('BATH', 'HEAT', 'SOLA')
                group by
                    cct.product_group,
                    cct.website
                """

cursor.execute(QS)

row = 1

while row:
    row = cursor.fetchone()
    print row
(u'SOLA', u'Beam LED', 5065.771499999995, 96.61683876775501, 162742.97946200587, 10194.260000000007, 18193.814072245008)
(u'BATH', u'Best Bathrooms', 2953.044100000001, 92.97357616908354, 41916.63833219, 2635.92, 8616.793372277998)
(u'HEAT', u'Best Bathrooms', 714.1080000000002, 242.6900400000002, 6608.969775007001, 411.96000000000004, 2156.7834558070003)
(u'SOLA', u'Best Eco Shop', 476.55, 90.5512, 0.0, 0.0, 0.0)
(u'HEAT', u'Best Heating', 67855.21000000008, 20803.223713668747, 1221384.6595271053, 75820.06999999998, 377841.516805281)
(u'HEAT', u'Best Heating DE', 764.414853, -74.59615568499949, 29173.095597537005, 1804.9900000000002, 8475.738008001008)
(u'HEAT', u'Best Heating IE', 511.684638, 145.72208938499998, 22200.070480626004, 1384.869999999999, 7635.299995903002)
(u'BATH', u'Beste Badezimmer', 605.427752, 136.0132520000001, 0.0, 0.0, 0.0)
(u'BATH', u'Big Bathroom Shop', 9055.997500000003, 1090.7121487611153, 194083.00040568007, 12434.420000000002, 24954.134240825995)
(u'HEAT', u'Big Bathroom Shop', 1637.3744000000004, 624.0700689273556, 22149.000000066, 1391.72, 8705.787840001)
(u'SOLA', u'Big Bathroom Shop', 0.0, 0.0, 5100.480000257999, 319.48999999999995, 1083.453844794)
(u'BATH', u'Cheap Suites', 6708.027599999998, 1218.3929213940442, 96801.99990245402, 6082.849999999999, 14238.596937036995)
(u'HEAT', u'Cheap Suites', 7877.586600000003, 3094.981148824446, 106569.44176593701, 6610.709999999997, 33754.975267268004)
(u'BATH', u'Hudson Reed CA', 2450.690545, 520.74435478, 37082.100495228, 2355.16, 12856.460342323011)
(u'HEAT', u'Hudson Reed CA', 198.955982, 43.108281999999996, 9353.568286207998, 593.02, 880.94902339)
(u'BATH', u'Hudson Reed DE', 7626.790740999998, 1737.584896192042, 101559.039541116, 6444.300000000002, 23292.634339761993)
(u'HEAT', u'Hudson Reed DE', 10397.459475000007, 3731.6650875108417, 113215.76626921303, 7008.19, 36837.993763557)
(u'SOLA', u'Hudson Reed DE', 0.0, 0.0, 4854.377637117, 304.08000000000004, 1737.9875217079998)
(u'BATH', u'Hudson Reed ES', 2748.9035340000005, 965.6333561828495, 19604.319998916995, 1216.6799999999998, 4734.42797947)
(u'HEAT', u'Hudson Reed ES', 1702.1610810000002, 589.3538634675374, 34249.141983546, 2118.4200000000005, 10209.710455291004)
(u'SOLA', u'Hudson Reed ES', 0.0, 0.0, 289.80000006, 18.16, 116.75621339399999)
(u'BATH', u'Hudson Reed FR', 47273.83856200002, 14384.879203607652, 347181.18108432164, 21603.31999999999, 69657.60552098295)
(u'HEAT', u'Hudson Reed FR', 19740.58251500001, 6704.351543201098, 226974.89734840702, 13947.230000000001, 66110.171342503)
(u'SOLA', u'Hudson Reed FR', 0.0, 0.0, 4854.377637116998, 304.08000000000004, 1737.7115216809996)
(u'BATH', u'Hudson Reed IT', 1520.260361, 221.10746836602254, 16166.474983499003, 1003.3399999999998, 3377.939853656)
(u'HEAT', u'Hudson Reed IT', 3123.910959, 667.0734018041572, 44038.997226871994, 2706.38, 10705.928342997002)
(u'SOLA', u'Hudson Reed IT', 0.0, 0.0, 289.80000005999995, 18.16, 116.75621339399997)
(u'BATH', u'Hudson Reed NL', 16583.111918, 5610.879630999159, 109605.65601838099, 6872.79, 21186.901077737002)
(u'HEAT', u'Hudson Reed NL', 10411.469125, 3712.277327632342, 111566.249953006, 6818.089999999997, 25728.021958158024)
(u'SOLA', u'Hudson Reed NL', 0.0, 0.0, 289.80000006, 18.16, 116.75621338999997)
(u'BATH', u'Hudson Reed US', 8481.261439000004, 3675.9046331535123, 183865.59700310792, 11516.219999999996, 71254.06636748099)
(u'HEAT', u'Hudson Reed US', 2097.8916030000005, 938.4614114663307, 45660.18595451701, 2833.7799999999993, 14384.518580478)
(u'BATH', u'Trueshopping', 5265.242654999999, 583.9883320369803, 79736.07721019002, 5047.579999999999, 17654.345117839996)
(u'HEAT', u'Trueshopping', 5519.389900000001, 1508.0805920000003, 74418.77915825396, 4634.490000000001, 21116.580455338986)
(u'SOLA', u'Trueshopping', 777.320599, 268.97614415250007, 0.0, 0.0, 0.0)
None