Category Num
-------------
x 1
x 1
x 2
x 3
y 1
y 1
z 1
z 1
z 2
SELECT Category, Count(*) AS Items
FROM SomeTable
GROUP BY Category
returns:
Category Num
-------------
x 4
y 2
z 3
SELECT Category, Count(Distinct Num) AS DistinctItems
FROM SomeTable
GROUP BY Category
returns:
Category Num
-------------
x 3
y 1
z 2
SELECT Category
FROM SomeTable
GROUP BY Category
SELECT Count(*) AS DistinctCategories
FROM Step1
SELECT Category, Num
FROM SomeTable
GROUP BY Category, Num
SELECT Category, Count(Num) AS DistinctNum
FROM Step1
GROUP BY Category
SELECT Count(z.Category) AS DistinctItems
FROM
(SELECT Category
FROM SomeTable
GROUP BY Category) AS z
SELECT z.Category, Count(z.Num)
FROM
(SELECT s.Category, s.Num
FROM SomeTable s
GROUP BY s.Category, s.Num) AS z
GROUP BY z.Category
SELECT Count(z.Category) AS DistinctItems
FROM [SELECT Category FROM SomeTable GROUP BY Category]. AS z
SELECT z.Category, Count(z.Num)
FROM [SELECT s.Category, s.Num FROM SomeTable s GROUP BY s.Category, s.Num]. AS z
GROUP BY z.Category
Function DCountDistinct(CountCols As String, Tbl As String, Optional Criteria As String = "")
' Function by Patrick G. Matthews
' Requires reference to Microsoft DAO library
' This function provides "domain aggregate"-type functionality similar to
' COUNT(DISTINCT ColumnName) in Transact-SQL (i.e., SQL Server's dialect). The function
' can be used in queries, forms, and reports in the Access UI and/or in VBA code once added
' to a regular VBA module
' CountColumns is a comma-delimited list of columns for which we are counting the distinct
' values (or combinations of values if 2+ columns are specified). Place field names in
' square brackets if they do not meet the customary rules for naming DB objects
' Tbl is the table/query the data are pulled from. Place table name in square brackets
' if they do not meet the customary rules for naming DB objects
' Criteria (optional) are the criteria to be applied in the grouping. Be sure to use And
' or Or as needed to build the right logic, and to encase text values in single quotes
' and dates in #
' Just like DCount, if there are no items found matching the criteria, DCountDistinct
' returns zero
Dim rs As DAO.Recordset
Dim SQL As String
' Enable error handler
On Error GoTo ErrHandler
' Build query that counts the number of distinct items (or combinations of items, if
' CountCols specifies 2+ columns) that meet the specified criteria. If no criteria are
' specified, then count goes against entire table
SQL = "SELECT Count(1) AS Result " & _
"FROM (" & _
"SELECT DISTINCT " & CountCols & " " & _
"FROM " & Tbl & " " & _
IIf(Criteria <> "", "WHERE " & Criteria, "") & _
")"
' Open recordset with result
Set rs = CurrentDb.OpenRecordset(SQL)
' Set return value
DCountDistinct = rs!Result
rs.Close
GoTo Cleanup
ErrHandler:
DCountDistinct = CVErr(Err.Number)
Cleanup:
Set rs = Nothing
End Function
SELECT DCountDistinct("Category", "SomeTable") AS DistinctItems
SELECT Category, DCountDistinct("Num", "SomeTable", "[Category] = '" & Category & "'")
FROM SomeTable
GROUP BY Category
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
Very nice article!
Seeing a number of these questions in the SQL topic areas, I am certain this will come in handy for those new to count(distinct) and possibly MS Access. As someone who was familiar with this previously, I was still thrilled to get a nice new function in the DCountDistinct() which gives a nice alternative in Jet SQL to sub-queries or multiple-step processes. As with the other domain functions, it seems well worth the minor overhead of adding VBA to the mix. :)
Thank you very much!
Voted a big YES above.
Respectfully yours,
Kevin
Commented:
-David251
Commented: