Select Count Distinct

  • 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_numbertinyint,

    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

  • 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

    [font="Verdana"]Thanks
    Chandra Mohan[/font]

  • 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 [font="Courier New"]co_number, contact[/font] 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

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply