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

时间:2016-04-20 21:15:01

标签: sql excel vba oledb

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

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"

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

3 个答案:

答案 0 :(得分:2)

考虑派生表的内部联接(即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]"

答案 1 :(得分:1)

尝试一下:

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

答案 2 :(得分:1)

您需要在两个查询中执行此操作。我已将第二个作为子句点放在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]"