SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Count distinct rows

    Is it possible to return the count of all distinct rows in a query?

    If I have a table like this:
    col1 col2
    1 100
    2 100
    3 300
    4 200
    5 100

    And run a query like this (dont know if it is right syntax):
    SELECT DISTINCT(col2) * FROM Table

    I would get something like this:
    1 100
    3 300
    4 200

    But I would like to get the count of each distinct row like this:
    1 100 3
    3 300 1
    4 200 1

    Is something like this possible? And if it is, how?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,215
    Mentioned
    58 Post(s)
    Tagged
    3 Thread(s)
    yes, it is possible
    Code:
    SELECT MIN(col1) AS scooby
         , col2
         , COUNT(*) AS doobie
      FROM daTable
    GROUP
         BY col2
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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