简体   繁体   中英

MS ACCESS SQL QUERY COUNT DISTINCT

I understand how to do a count distinct when it's simple like this:

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;

However, I'm not sure how to add a count distinct when it's a more complicated query without effecting the other data. In the below query, I want the last item (NumDays) to be a distinct count of 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;

EDIT : [AllItemsDateRange] is basically a list of transactions. Each transaction has a date on it. I want to summarize the data by ACH_Processor. So getting the summary of of count and sum of debits and credits is easy enough. The hard part is getting the count of distinct days on which a transaction was processed by that ACH_Processor so I can later calculate daily averages.

count(Distinct fieldname)Access中不支持的功能:您必须这样做才能使其与众不同: 如何在Access查询中计算字段中的唯一项?

Revise the first query to produce the distinct count ( NumDays ) for the same GROUP BY as the second query.

Remove NumDays from the second query.

Then you can INNER JOIN the two on those fields which were included in the GROUP BY .

But reading the question again, it sounds like you actually do want a different GROUP BY for each query. In that case, base the join on the field(s) which are included in both: ACH_Processor

That can still work, but the same NumDays value could be repeated across more than one row of the final query. But maybe that's what you want ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM