Results 1 to 5 of 5

Thread: count from distinct

  1. #1
    Join Date
    Apr 2006
    Posts
    178

    count from distinct

    Hello

    for mS SQL 2000

    with

    SELECT DISTINCT Name, Region
    FROM Groups
    GROUP BY Name, Region


    I get 254 rows

    but how can I get the COUNT only ?

    something like

    SELECT COUNT(SELECT DISTINCT Name, Region
    FROM Groups
    GROUP BY Name, Region) AS CPT FROM Groups


    i must get 254

    thank you for helpings

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    You can get it with @@rowcount like:

    SELECT DISTINCT Name, Region
    FROM Groups
    GROUP BY Name, Region

    select @@rowcount

  3. #3
    Join Date
    Apr 2006
    Posts
    178
    never heard before !

    thank you

  4. #4
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    If you don't care about rows returned and interested in count only then

    select count(*)
    from (SELECT DISTINCT Name, Region
    FROM Groups
    GROUP BY Name, Region
    ) as A

  5. #5
    Join Date
    Dec 2004
    Posts
    502
    Just an FYI: you don't need to do a GROUP BY if you are using DISTINCT. Also, you can get the count of distinct records this way, although it isn't necessarily faster:

    SELECT COUNT(DISTINCT name + region) FROM Groups

    (this only works if the columns are char or varchar - if not, you have to convert them to one of those datatypes)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •