Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select Count Distinct


Select Count Distinct

Author
Message
armoredeagle
armoredeagle
Old Hand
Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)Old Hand (352 reputation)

Group: General Forum Members
Points: 352 Visits: 758
SQL Server Gurus:

I often have the need to get a count of distinct values in multiple columns. I get a syntax error. I'm not sure why it can't do this and if there is a straigtforward way to make it work. For example, if I have this table:

Create table Test_contact
(co_number         tinyint,
contact          char(5),
call_day          char(3))

Insert into Test_contact Values (1, 'Dave', 'Mon')
Insert into Test_contact Values (2, 'Steve', 'Mon')
Insert into Test_contact Values (2, 'Bill', 'Wed')
Insert into Test_contact Values (3, 'Amy', 'Tue')
Insert into Test_contact Values (3, 'Amy', 'Thu')

This works:

select count(distinct co_number)
from Test_contact

And this works (and also gives a count of the rows returned):

select distinct co_number, contact
from Test_contact

But if I put them together I get a syntax error (Incorrect syntax near ','.):

select count(distinct co_number, contact)
from Test_contact

Why can't it do this? This is the way I have come up with to get around it but I am wondering if there is a better way:

select count(*)
from
(select distinct co_number, contact
from Test_contact) t



Chand00
Chand00
Mr or Mrs. 500
Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)Mr or Mrs. 500 (532 reputation)

Group: General Forum Members
Points: 532 Visits: 1248
Hi,

Count is an aggreate function which accepts only one input parameter. In your query you are trying to input two columns as input to the function. So the below query is giving you error.

select count(distinct co_number, contact)
from Test_contact

Refer
http://msdn.microsoft.com/en-us/library/ms175997.aspx

Thanks
Chandra Mohan

kevriley
kevriley
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2751 Visits: 2606
ChandraMohan Nandula (7/15/2009)

Count is an aggreate function which accepts only one input parameter. In your query you are trying to input two columns as input to the function. So the below query is giving you error.

select count(distinct co_number, contact)
from Test_contact

Refer
http://msdn.microsoft.com/en-us/library/ms175997.aspx




Chandra - I think tknight understands where the error is coming from, the question I think was more of a conceptual one - why can't a query be constructed like this.
Also the error is actually fired because COUNT accepts an expression, and a column list [which is what co_number, contact is], is not a valid expression.


tknight - the issue comes from the confusion in the 2 queries:

In the first one, you are counting the number of rows in a table
In the second one, you are returning a result set, and simply counting the rows, so it seems like you should be able to simply glue the 2 together and get your desired result.

The solution you have discovered is actually what you are trying to do : execute the query, and count the results.


Another horrible workaround would be to create a derived column made up of the multiple columns, and COUNT(Distinct that) i.e.

select 
   count(distinct cast(co_number as varchar) + cast(contact as varchar))
from Test_contact





Kev
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search