Excel/ADO/VBA:计数返回不正确的结果

Posted

技术标签:

【中文标题】Excel/ADO/VBA:计数返回不正确的结果【英文标题】:Excel/ADO/VBA: Count returning incorrect results 【发布时间】:2016-04-20 21:15:01 【问题描述】:

我有一个包含以下代码的宏:

sSQL = "select [Folio Type], [Folio ID], [Departure Date]," & _
       "        Sum([Folio Total]), Count([Folio ID])" & _
       " from [Individual Folios$B2:O150000]" & _
       " group by [Folio Type], [Departure Date], [Folio ID], [Folio Total]" & _
       " having Sum([Folio Total]) <> 0"

上面的代码给我一个错误的计数,每个[Folio ID] 都是 1。我想获得给定[Folio ID] 的总数,而不管[Departure Date]。当我删除一些字段时,我得到了不同的结果 - 以下代码似乎工作正常:

 sSQL = "select [Folio ID], Count([Folio ID]), Sum([Folio Total])" & _
        " from [Individual Folios$B2:O150000]" & _
        " group by [Folio ID], [Folio Total]" & _
        " having sum([Folio Total]) <> 0"

我需要弄清楚如何在重新添加已删除的字段后获得与上述相同的结果。我的猜测是该查询通过考虑所有字段来计算出现次数。如果是这种情况,有什么简单的解决方法吗?

【问题讨论】:

【参考方案1】:

考虑派生表的内部连接(即FROM 子句中的子查询)。

标准 SQL (概念图)

SELECT t1.[Folio Type], t1.[Folio ID], t1.[Departure Date], 
       t2.FolioCount, t2.FolioSum  
FROM    
        (SELECT [Folio Type], [Folio ID], [Departure Date],
                [Folio Total]   
         FROM [Individual Folios$B2:O150000]
        ) AS t1 
INNER JOIN  
        (SELECT [Folio ID], Count([Folio ID]) As FolioCount,   
                    SUM([Folio Total]) As FolioSum   
             FROM [Individual Folios$B2:O150000]   
             GROUP BY [Folio ID], [Folio Total]   
             HAVING SUM([Folio Total]) <> 0
         ) As t2 
ON t1.[Folio ID] = t2.[Folio ID] 
AND t1.[Folio Total] = t2.[Folio Total]

VBA 嵌入字符串:

 sSQL = "SELECT t1.[Folio Type], t1.[Folio ID], t1.[Departure Date],"
 sSQL = sSQL &  " t2.FolioCount, t2.FolioSum"
 sSQL = sSQL &  " FROM"
 sSQL = sSQL &  "  (SELECT [Folio Type], [Folio ID], [Departure Date]" 
 sSQL = sSQL &  "   FROM [Individual Folios$B2:O150000]) AS t1" 
 sSQL = sSQL &  " INNER JOIN"
 sSQL = sSQL &  "  (SELECT [Folio ID], Count([Folio ID]) As FolioCount,"
 sSQL = sSQL &  "          SUM([Folio Total]) As FolioSum" 
 sSQL = sSQL &  "   FROM [Individual Folios$B2:O150000]"
 sSQL = sSQL &  "   GROUP BY [Folio ID], [Folio Total]"
 sSQL = sSQL &  "   HAVING SUM([Folio Total]) <> 0) As t2"
 sSQL = sSQL &  " ON t1.[Folio ID] = t2.[Folio ID]"
 sSQL = sSQL &  " AND t1.[Folio Total] = t2.[Folio Total]"

【讨论】:

【参考方案2】:

您需要在两个查询中执行此操作。我已将第二个作为子查询放在 FROM 子句中。

SELECT 
     a.[Folio Type]
     ,a.[Folio ID]
     ,a.[Departure Date]
     ,b.FolioCount
     ,b.FolioSum  
FROM   
    [Individual Folios$B2:O150000] a    
INNER JOIN  
        (SELECT 
              [Folio ID], [Folio Type]
              ,Count([Folio ID]) As FolioCount
              ,SUM([Folio Total]) As FolioSum   
         FROM 
              [Individual Folios$B2:O150000]
         GROUP BY 
              [Folio ID]
              ,[Folio Type]   
         HAVING SUM([Folio Total]) <> 0
         ) b
ON a.[Folio ID] = b.[Folio ID] 

使你的字符串等于这个查询:

 sSQL = "SELECT a.[Folio Type],a.[Folio ID],a.[Departure Date]" _
      & ",b.FolioCount,b.FolioSum" _  
      & " FROM [Individual Folios$B2:O150000] a" _    
      & " INNER JOIN" _  
      & "     (SELECT [Folio ID], [Folio Type] " _
      & "      Count([Folio ID]) As FolioCount,SUM([Folio Total]) As FolioSum" _   
      & "      FROM [Individual Folios$B2:O150000]" _
      & "      GROUP BY [Folio ID], [Folio Type]" _   
      & "      HAVING SUM([Folio Total]) <> 0) b"_
      & " ON a.[Folio ID] = b.[Folio ID]"

【讨论】:

这个答案和 Parfait 的类似,但我选择了这个,因为出于某种原因我不得不删除他提供的最后一行代码:sSQL = sSQL &amp; " AND t1.[Folio Total] = t2.[Folio Total] 我补充说最后一次加入是因为你原来的GROUP BY[Folio ID][Folio Total],我几乎问过!【参考方案3】:

试试这个:

select  max([Folio Type]),
        [Folio ID],
        max([Departure Date]),
        sum([Folio Total]),
        count([Folio ID])
from    [Individual Folios$B2:O150000]
group by [Folio ID],
        [Folio Total]
having  sum([Folio Total]) <> 0

【讨论】:

以上是关于Excel/ADO/VBA:计数返回不正确的结果的主要内容,如果未能解决你的问题,请参考以下文章

为啥在使用“.count”返回 Swift 下标计数时收到不正确的数组计数?

计数结果不正确

TSQL:外部连接表的计数产生不正确的结果

行计数使用 RaptureXML 返回不正确的数字

SOQL:避免使用 NULL 结果计数,而是返回 0(零)

Mysql 行计数为连接表返回不正确的值[关闭]