Home All Groups Group Topic Archive Search About

please help on count(distinct <expression>)

Author
14 Jul 2006 6:44 PM
Carlos
Hi all,

  the sql parser does not like when trying to execute a query that
has more than one column to be distinct within the count function.
i.e.

select count ( distinct a.ssn, a.term ) from
mytable

when I just leave one column (i.e. ssn), the count is executed normally,
but it does not like to consider more columns.

Does someone knows why?

Thanks in advance,

                    Carlos.

Author
14 Jul 2006 6:52 PM
Arnie Rowland
Change it to:

SELECT DISTINCT
     a.SSN
   , a.Term
   , count(1) AS 'Total Distinct Values'
FROM Mytable


--
Arnie Rowland
"To be successful, your heart must accompany your knowledge."



Show quote
"Carlos" <ch_sa***@yahoo.com> wrote in message news:eW2BWW3pGHA.3908@TK2MSFTNGP05.phx.gbl...
>
>
> Hi all,
>
>  the sql parser does not like when trying to execute a query that
> has more than one column to be distinct within the count function.
> i.e.
>
> select count ( distinct a.ssn, a.term ) from
> mytable
>
> when I just leave one column (i.e. ssn), the count is executed normally,
> but it does not like to consider more columns.
>
> Does someone knows why?
>
> Thanks in advance,
>
>                    Carlos.
>
>
Author
14 Jul 2006 6:54 PM
Aaron Bertrand [SQL Server MVP]
That's because COUNT takes exactly one argument.

How about

SELECT COUNT(*) FROM (SELECT ssn, term FROM mytable GROUP BY ssn, term) x



Show quote
"Carlos" <ch_sa***@yahoo.com> wrote in message
news:eW2BWW3pGHA.3908@TK2MSFTNGP05.phx.gbl...
>
>
> Hi all,
>
>  the sql parser does not like when trying to execute a query that
> has more than one column to be distinct within the count function.
> i.e.
>
> select count ( distinct a.ssn, a.term ) from
> mytable
>
> when I just leave one column (i.e. ssn), the count is executed normally,
> but it does not like to consider more columns.
>
> Does someone knows why?
>
> Thanks in advance,
>
>                    Carlos.
>
Author
14 Jul 2006 8:55 PM
Carlos
Thank you all for your help. SQL server books online describes that
an expression can acompany the count, so I thought that it would be
possible. I used something similar to Aaron's answer and it succeeded.

Thanks again!

             Carlos.

Show quote
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:%234n20b3pGHA.4116@TK2MSFTNGP03.phx.gbl...
> That's because COUNT takes exactly one argument.
>
> How about
>
> SELECT COUNT(*) FROM (SELECT ssn, term FROM mytable GROUP BY ssn, term) x
>
>
>
> "Carlos" <ch_sa***@yahoo.com> wrote in message
> news:eW2BWW3pGHA.3908@TK2MSFTNGP05.phx.gbl...
>>
>>
>> Hi all,
>>
>>  the sql parser does not like when trying to execute a query that
>> has more than one column to be distinct within the count function.
>> i.e.
>>
>> select count ( distinct a.ssn, a.term ) from
>> mytable
>>
>> when I just leave one column (i.e. ssn), the count is executed normally,
>> but it does not like to consider more columns.
>>
>> Does someone knows why?
>>
>> Thanks in advance,
>>
>>                    Carlos.
>>
>
>

AddThis Social Bookmark Button