【问题标题】:SQL query returns different results when run from Excel从 Excel 运行 SQL 查询返回不同的结果
【发布时间】:2013-12-19 07:50:09
【问题描述】:

当直接在 SQL Server Management Studio 中运行和从 Excel 运行(使用 SQLOLEDB 提供程序)时,查询返回不同的结果。两者都返回一个包含 12 行(每月一个)和两列(日期和复杂计数)的表,但 Excel 结果集中的某些数值不正确。

我很高兴向您展示完整的细节,但首先您能建议什么类型的事情会导致这种差异吗?

版本:SQL Server 10.50.4000、Excel 2013

编辑:完整的细节...

select cast(ActualDate as date) as 'Start of rolling 12 months'
    , count(distinct x.con_id) as 'Total Sponsors'
from DateTimeDimV4.dbo.datedim
    join (select cm_id, con_id
            , cm.start_date as activation_date
            , isnull(stop_date, getdate()) as stop_date
        from cm 
            join con_act ca on ca.con_act_db_id = cm.con_act_db_id and ca.con_act_id = cm.con_act_id
        where 
            ca.code = 'corres'
        ) X on cast(activation_date as date) <= cast(ActualDate as date) 
                and cast(stop_date as date) > cast(ActualDate as date)
where 
    --ActualDate = eomonth(ActualDate) -- only works in SQL 2012, so instead we use ...
    ActualDate = DATEADD(dd, -DAY(DATEADD(mm, 1, ActualDate)), DATEADD(mm, 1, ActualDate))
    and ActualDate >= '31 Jul 2012' and ActualDate <= '30 June 2013'
group by cast(ActualDate as date)
order by cast(ActualDate as date)

在 SSMS 中,这将返回 ...

Start of rolling 12 months  Total Sponsors
2012-07-31  862
2012-08-31  872
2012-09-30  872
2012-10-31  880
2012-11-30  876
2012-12-31  878
2013-01-31  882
2013-02-28  888
2013-03-31  887
2013-04-30  887
2013-05-31  920
2013-06-30  933

但在 Excel 中我得到...

Start of rolling 12 months  Total Sponsors
2012-07-31  862
2012-08-31  872
2012-09-30  872
2012-10-31  880
2012-11-30  876
2012-12-31  878
2013-01-31  882
2013-02-28  887
2013-03-31  887
2013-04-30  887
2013-05-31  887
2013-06-30  887

请注意,前七行相同,但 Excel 在后五行中重复了相同的错误值。

另外请注意,如果我将文字日期从“2012 年 7 月 31 日”和“2013 年 6 月 30 日”更改为“2011 年 7 月 31 日”和“2012 年 6 月 30 日”,那么这两个环境会产生彼此相同的结果。

【问题讨论】:

  • 您需要展示完整的细节。否则,我们不知道数据类型、“不正确”的含义等。请在您的问题中提供尽可能详细的信息。
  • 不正确的意思?值错误四舍五入,完全错误的计算,提供更多细节
  • 我已在问题中添加了详细信息。我在那里展示的代码是一个缩减版本,在仍然显示问题的同时,尽可能多地删除了复杂性。在这种简单程度下,我很可能无需使用子选择就可以改写它,但我认为改写不适用于更复杂的实际情况。
  • 您是否将查询直接放入 Excel 中?像这样? spreadsheetsmadeeasy.com/excel-create-pivot-table-using-sql
  • 如果它在 SQL 中工作正常但在 Excel 中不能正常工作,我猜这与 Excel 对日期的“智能”解释有关。您可以尝试两件事: 1. 使用 SQL Profiler 捕获和检查 Excel 正在提交的 SQL。 2. 更改您的查询以将日期返回为看起来与日期完全不同的内容,即“ZZZ20130731”,然后查看它的作用。不是一个解决方案,只是一个解决方案的旅程。如果您想了解更多详细信息,请回复。

标签: sql-server excel


【解决方案1】:

尝试改变:

and ActualDate >= '31 Jul 2012' and ActualDate <= '30 June 2013'

到:

and ActualDate >= '2012-07-31' and ActualDate <= '2013-06-30'

我的逻辑:我认为 MS Query 对字面日期的解释与 SSMS 不同。不过,我无法证明这一点,因为我在 MS Query 上找不到任何文档。 从头开始​​。我们姑且称之为预感吧。

我通常坚持所有日期文字的 ISO 日期格式。

另外,你为什么缩写 July 而不是 June?

【讨论】:

  • 不错的预感,但没什么区别。
  • 六月 - 七月异常只是一个错字。但在 SSMS 中没有区别。
  • :) 我试过了。抱歉@MattClarke,不知道还有什么可能。这很奇怪。
【解决方案2】:

确实是野餐!很抱歉浪费了您的时间。

Excel 版本正在对正确的服务器执行查询,但数据库是错误的。它是针对一个备份数据库执行的,该数据库的名称与实时数据库的名称只有一个字符不同。

【讨论】:

    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 2012-05-08
    • 1970-01-01
    • 2011-11-08
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多