• 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