Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Select Count Distinct Expand / Collapse
Author
Message
Posted Wednesday, July 15, 2009 7:27 AM


SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 11:57 AM
Points: 293, Visits: 682
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





Post #753428
Posted Wednesday, July 15, 2009 8:25 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Friday, July 25, 2014 12:52 PM
Points: 530, Visits: 1,134
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
Post #753508
Posted Wednesday, July 15, 2009 9:10 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 8:51 AM
Points: 2,716, Visits: 2,459
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
Post #753572
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse