I have a table like this in MS Access database:
ID Date CustomerID InvoiceNo ItemNo Amount
1 01/01/2007 A 1 AAA 1
2 01/01/2007 A 1 BBB 2
3 01/01/2007 A 2 CCC 3
6 01/01/2007 B 5 AAA 6
7 01/01/2007 A 6 BBB 7
8 01/01/2007 B 5 BBB 8
I want to get disctinct count of InvoiceNo while summing up Amount by Date and CustomerID in one query. Firstly, I tried to use correlated query to count unique records but it failed.
The result of Count is not a unique count. Can anyone tell me what's wrong with this correlated query?
After hours of googling on internet, the solution I got is equivalent to querying on top of the resulting table from another query.
My final query is
Is there a better solution than this in Access?
Thanks,
Huyeote
ID Date CustomerID InvoiceNo ItemNo Amount
1 01/01/2007 A 1 AAA 1
2 01/01/2007 A 1 BBB 2
3 01/01/2007 A 2 CCC 3
6 01/01/2007 B 5 AAA 6
7 01/01/2007 A 6 BBB 7
8 01/01/2007 B 5 BBB 8
I want to get disctinct count of InvoiceNo while summing up Amount by Date and CustomerID in one query. Firstly, I tried to use correlated query to count unique records but it failed.
Code:
SELECT A.Date, A.CustomerID, (SELECT COUNT(B.InvoiceNo) FROM Table1 B WHERE B.Date = A.Date AND B.CustomerID = A.CustomerID ) AS CountOfDistInvNo, Sum(A.Amount) AS SumOfAmount
FROM Table1 A
GROUP BY A.Date, A.CustomerID;

After hours of googling on internet, the solution I got is equivalent to querying on top of the resulting table from another query.
My final query is
Code:
SELECT B.Date, B.CustomerID, Count(B.InvoiceNo) AS CountOfInvoiceNo, Sum(B.SumOfAmount) AS SumOfAmount
FROM (SELECT A.Date, A.CustomerID, A.InvoiceNo, SUM(A.Amount) AS SumOfAmount FROM Table1 A GROUP BY A.Date, A.CustomerID, A.InvoiceNo) AS B
GROUP BY B.Date, B.CustomerID;

Thanks,
Huyeote