由于 SQL 日期比较设置错误,SQL 结果不准确

The SQL result is inaccurate because of wrong SQL date comparison setting

我在 excel 中 运行 SQL 在 excel-VBA 中使用 ADO。结果显示在 excel 工作表上。

工作表 2014,2015,2016,2017 中有一个名为 Date 的字段

数据示例:2/1/2014,7/1/2014,23/10/2014

这个字段的数据类型是day/month/year。日期由=DATE(cell1,cell2,cell3) -year,month,day 合并。tables中的日期应该都是纯日期,因为我将 3 个单元格(年、月、日)合并为 1 个字段(日期)

'Dim two date variables 

Dim fromDate As Date
Dim toDate As Date

'Dim 4 integer variables   

Dim fromyear As Integer
Dim toyear As Integer
Dim frommonth As Integer
Dim ToMonth As Integer

'add combobox value
With FromYearC
.AddItem 2014
.AddItem 2015
.AddItem 2016
.AddItem 2017
End With

'add combobox value 
With FromMonthC
.AddItem 1
.AddItem 2
.AddItem 3
.AddItem 4
.AddItem 5
.AddItem 6
.AddItem 7
.AddItem 8
.AddItem 9
.AddItem 10
.AddItem 11
.AddItem 12
End With

'Store combo box value into these 4 integer variables 

fromyear = FromYearC.Value
frommonth = FromMonthC.Value
toyear = ToYearC.Value
ToMonth = ToMonthC.Value

'Now i want to combine these variables and store into fromDate and toDate
fromDate = DateSerial(fromyear, frommonth, 1)
toDate = DateSerial(toyear, ToMonth + 1, 1)

'it is still wrong , no matter orginal date format or new date format"dd/mm/yyyy"
  fromDate = Format(fromDate, "dd/mm/yyyy")
    toDate = Format(toDate, "dd/mm/yyyy")

VBA 用户界面: 现在我想设置从日期到日期之间的日期范围(包括在内)

我建立 SQL 参考:

 ' This is the new SQL string

WHERE date >= #" & fromdate & "# AND date<#" & toDate & "#"

问题:

现在 SQL 结果完全错误(仍然有输出)。我猜 SQL where 子句有问题。由于 SQL 本来是对的,因此 SQL 因 where-date selection 子句而变得错误。

或者我尝试使用 where-between 子句。这仍然是错误的。

date between  #" & fromDate & "#  and  #" & toDate & "#

完整的 SQL 在这里,但我认为它太长了

    SELECT  officer ,NULL, SUM(IIF( isnumeric(mkt) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) , SUM(IIF( isnumeric(Non) = true and Survey='CPI' and Activity='FI' and Outcome= 'C', Totalmin, 0 )/468) ,NULL ,NULL , IIF(ISNULL(sum(mkt)),0,sum(mkt)),Sum(Non),sum(ICP),(sum(mkt)+Sum(Non)+sum(ICP) )  ,NULL,NULL,NULL,count(IIF( Survey='CPI' and Activity='FI' ,Totalmin, NULL )),NULL,count(IIF(  Survey='CPI' and Activity='FI' and  (Outcome ='C' OR Outcome='D'OR Outcome='O') , Totalmin, NULL )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' ,Totalmin, 0 )),NULL,SUM(IIF( Survey='CPI' and Activity='FI' and (Outcome ='C' OR Outcome='D') ,Totalmin, 0 )) From  (select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2014$] UNION ALL  select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2015$]  UNION ALL  select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from 
[2016$] UNION ALL  select officer ,rank ,year ,month ,day , survey ,activity ,outcome ,mkt,non,totalmin,ICP ,date from [2017$])as table3 where officer is not null and officer <> '' and officer <> ' '  and  date >= #" & fromDate & "# AND date<#" & toDate & "#" group by officer

更新

我尝试使用硬编码

 and date >= #1-3-2016# AND date<#31-3-2016#

测试 SQL 。

它像原来的 SQL 一样失败了。它可以输出结果,但结果是错误的。

我想这与 UNION ALL 有关吗? 当我加入所有 4 tables 记录成为一个大 table .

更新 2

我相信它与 UNION ALL 无关。 由于 excel 列日期组合在 3 个单元格中 - 年、月、日,我再次复制并粘贴该值。SQL 结果发生了变化。但是,结果仍然不正确。

然后我尝试使用硬代码来测试。 例如,我尝试 select 2016 年 3 月的记录。 使用此 where 子句:

and month=3 and year=2016

没关系。

然后我尝试使用下面的 where 语句。这不行。

date >= #2016-03-01# AND date<#2016-04-01#

那么我认为这应该是 excel 或 VBA 中的日期数据类型问题。

然后,我尝试在下面进行这些测试:

对于Excel,原来的数据类型是date,我改成string,numeric,general...不行

日期样本:6/1/2016 字符串 sample:42375 数字样本:42375.00

对于VBA,我尝试交换月份和日期。 --> #2016-01-03# 还是不行

您需要为 fromDate 和 toDate 日期创建字符串变量并将它们传递给 WHERE 子句。我建议你使用 ISO 日期格式。除此之外,为了避免列名日期出现问题,让我们尝试将日期列括在方括号中(对联合表也这样做),如下所示:

  fromDateStr = Format(fromDate, "yyyy-mm-dd")
    toDateStr = Format(toDate, "yyyy-mm-dd")

"WHERE [date] >= #" & fromDateStr & "# AND [date]<#" & toDateStr & "#"

附带说明一下,当 toMonth 是 12 月时,这个表达式 DateSerial(toyear, ToMonth + 1, 1) 会发生什么情况?你不应该使用 DateAdd 函数吗?

toDate = DateAdd("m", 1, DateSerial(toyear, ToMonth, 1))