SYNTAX sql...

User avatar
sal21
PlatinumLounger
Posts: 4369
Joined: 26 Apr 2010, 17:36

SYNTAX sql...

Post by sal21 »

Is thios correct to count a distinct value in field?

SELECT COUNT(DISTINCT(CAR)) FROM CAR_S

have error :scratch:

User avatar
HansV
Administrator
Posts: 78622
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SYNTIAX sql...

Post by HansV »

In SQL Server, you could use

SELECT COUNT(DISTINCT CAR) FROM CAR_S

but Microsoft Access doesn't support this syntax. You can use the following instead:

SELECT COUNT(CAR) FROM (SELECT DISTINCT CAR FROM CAR_S)

or

SELECT COUNT(CAR) FROM (SELECT CAR FROM CAR_S GROUP BY CAR)
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4369
Joined: 26 Apr 2010, 17:36

Re: SYNTIAX sql...

Post by sal21 »

HansV wrote:In SQL Server, you could use

SELECT COUNT(DISTINCT CAR) FROM CAR_S

but Microsoft Access doesn't support this syntax. You can use the following instead:

SELECT COUNT(CAR) FROM (SELECT DISTINCT CAR FROM CAR_S)

or

SELECT COUNT(CAR) FROM (SELECT CAR FROM CAR_S GROUP BY CAR)

TKS!
work.

But in order of speeding, peraph a group by and count have the best result, or not?

User avatar
HansV
Administrator
Posts: 78622
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SYNTIAX sql...

Post by HansV »

You'd have to test it on a large table. I think the difference will be small.
Best wishes,
Hans