Home All Groups Group Topic Archive Search About

Having problems with distinct and count

Author
5 Jan 2006 4:31 PM
Sai
Hi,


Here is the query I am trying to achieve and having syntax issues


Select count(distinct name, number) from results.


To replicate the situation use the following SQL


create table results (name varchar(100), number int)
insert into results values ('test1', 1)
insert into results values ('test1', 1)
insert into results values ('test1', 1)
insert into results values ('test2', 2)
insert into results values ('test2', 2)
insert into results values ('test2', 2)


Basically the return of the query should be 2. I can achieve this by
doing following query
select count(*) from
(select distinct [name], [number] from results) a


but I want to do it one query as the later query is a big hit on the
performance.


On a large sample of data the second query takes around 2 seconds.

Any help would be appreciated.

Thanks
SAI

Author
5 Jan 2006 4:42 PM
Alexander Kuznetsov
an index on (name, number) might speed it up

AddThis Social Bookmark Button