MS ACCESS SQL QUERY COUNT DISTINCT

时间:2014-01-17 21:12:12

标签: sql ms-access count distinct

我理解如何在这样简单的情况下进行计数:

SELECT AllItemsDateRange.Processors.ACH_Processor, COUNT(*) AS NumDays
FROM (SELECT DISTINCT AllItemsDateRange.Processors.ACH_Processor, AllItemsDateRange.SubmitDate FROM AllItemsDateRange)  AS T1
GROUP BY AllItemsDateRange.Processors.ACH_Processor;

但是,我不确定如何在不影响其他数据的情况下更复杂的查询时添加计数。在下面的查询中,我希望最后一项(NumDays)是AllItemsDateRange.Processors.ACH_Processor,AllItemsDateRange.SubmitDate的独特计数。

SELECT AllItemsDateRange.Processors.ACH_Processor, AllItemsDateRange.ExposureLimit, AllItemsDateRange.Footprint, Sum(IIf([AllItemsDateRange].[DebitAmount]>0,1,0)) AS Debits, Sum(AllItemsDateRange.DebitAmount) AS DebitAmt, Sum(IIf([AllItemsDateRange].[CreditAmount]>0,1,0)) AS Credits, Sum(AllItemsDateRange.CreditAmount) AS CreditAmt, Sum(IIf(([AllItemsDateRange].[ReturnDate]>0) And ([AllItemsDateRange].[DebitAmount]>0),1,0)) AS DebitReturns, Sum(IIf(([AllItemsDateRange].[ReturnDate]>0) And ([AllItemsDateRange].[CreditAmount]>0),1,0)) AS CreditReturns, Sum(IIf([AllItemsDateRange].[ReturnDate]>0,[AllItemsDateRange].[CreditAmount]+[AllItemsDateRange].[DebitAmount],0)) AS ReturnAmt, Sum(IIf((([AllItemsDateRange].[ReturnCode]="R05") Or ([AllItemsDateRange].[ReturnCode]="R07") Or ([AllItemsDateRange].[ReturnCode]="R10") Or ([AllItemsDateRange].[ReturnCode]="R29") Or ([AllItemsDateRange].[ReturnCode]="R51")) And ([AllItemsDateRange].[DebitAmount]>0),1,0)) AS UnauthorizedReturns, Sum(IIf((([AllItemsDateRange].[ReturnCode]="R05") Or ([AllItemsDateRange].[ReturnCode]="R07") Or ([AllItemsDateRange].[ReturnCode]="R10") Or ([AllItemsDateRange].[ReturnCode]="R29") Or ([AllItemsDateRange].[ReturnCode]="R51")) And ([AllItemsDateRange].[DebitAmount]>0),[DebitAmount],0)) AS UnauthorizedReturnAmt, COUNT(AllItemsDateRange.SubmitDate) AS NumDays
    FROM AllItemsDateRange
    GROUP BY AllItemsDateRange.Processors.ACH_Processor, AllItemsDateRange.ExposureLimit, AllItemsDateRange.Footprint
    ORDER BY AllItemsDateRange.Footprint, AllItemsDateRange.ExposureLimit DESC , AllItemsDateRange.Processors.ACH_Processor DESC;

修改: [AllItemsDateRange]基本上是一个事务列表。每笔交易都有一个日期。我想通过ACH_Processor汇总数据。因此,获得计数和借方和贷方总和的摘要很容易。困难的部分是得到ACH_Processor处理交易的不同日期的计数,以便我以后可以计算每日平均值。

2 个答案:

答案 0 :(得分:0)

count(Distinct fieldname)访问权限不支持的功能:您必须执行此操作才能获得区别:How do I count unique items in field in Access query?

答案 1 :(得分:0)

修改第一个查询以生成与第二个查询相同的NumDays的非重复计数(GROUP BY)。

从第二个查询中删除NumDays

然后,您可以在INNER JOIN中包含的字段GROUP BY GROUP BY

但是再次阅读这个问题,听起来你确实希望每个查询都有不同的ACH_Processor。在这种情况下,将连接基于包含在NumDays

中的字段

这仍然有效,但可以在最终查询的多行中重复相同的{{1}}值。但也许这就是你想要的......