How do I count unique items in field in Access que

2019-01-04 13:30发布

My Table: table1

ID  Name  Family
1   A     AA
2   B     BB
3   A     AB
4   D     DD
5   E     EE
6   A     AC

SQL command on Access:

select count(*) from table1

Output: ------------> True
6 row(s)

I tried to count unique names:

Expected output: 4 row(s)

select count(distinct Name) from table1

Output on Access: ------------> Error

What changes do I need to make to my query?

标签: sql ms-access
2条回答
我命由我不由天
2楼-- · 2019-01-04 13:54

Try this

SELECT Count(*) AS N
FROM
(SELECT DISTINCT Name FROM table1) AS T;

Read this for more info.

查看更多
萌系小妹纸
3楼-- · 2019-01-04 14:10

Access-Engine does not support

SELECT count(DISTINCT....) FROM ...

You have to do it like this:

SELECT count(*) 
FROM
(SELECT DISTINCT Name FROM table1)

Its a little workaround... you're counting a DISTINCT selection.

查看更多
登录 后发表回答