Web Analytics Made Easy -
StatCounter Select Most Common - CodingForum

Announcement

Collapse
No announcement yet.

Select Most Common

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Select Most Common

    if I have a table with lots of product IDs (occuring multiple times)
    how would I select for example the top 5 most common entries.
    Basically the table stores product requests and I want to select the most popular products.
    http://www.hazelryan.co.uk

  • #2
    Try something like

    Code:
    SELECT
     COUNT(product_id) AS prodcount
    ,prodname
    FROM
     table
    GROUP BY
     prodname
    ORDER BY
     prodcount
      DESC

    Comment


    • #3
      could I add a top to that?
      like:
      Code:
      SELECT 
      TOP 5 COUNT(product_id) 
      AS prodcount, prodname
      FROM
       table
      GROUP BY
       prodname
      ORDER BY
       prodcount
        DESC
      http://www.hazelryan.co.uk

      Comment


      • #4
        ..just tried it on another DB (the one I'm working on doesnt exist yet ) and it works great- exactly what I wanted!
        http://www.hazelryan.co.uk

        Comment

        Working...
        X