SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist jkh1978's Avatar
    Join Date
    May 2001
    Location
    Northern Virginia
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with a query

    I have a list of data. I want to know which value appears in the data the most often, and the percentage of it appearing. I am having trouble writing the sql statement with mysql.

  2. #2
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Could you please post the table(s) structure.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com

  3. #3
    SitePoint Evangelist jkh1978's Avatar
    Join Date
    May 2001
    Location
    Northern Virginia
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by westmich
    Could you please post the table(s) structure.
    KeyCol | DataCol
    ----------------
    A | 1
    B | 2
    C | 1
    D | 1
    E | 5
    F | 1

    I want to return that DataCol's most used value is 1, and that it is in 67% of the rows.

  4. #4
    SitePoint Evangelist jkh1978's Avatar
    Join Date
    May 2001
    Location
    Northern Virginia
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    You can see the table structure is very simple, but I cannot figure out the way to do this.

  5. #5
    SitePoint Wizard westmich's Avatar
    Join Date
    Mar 2000
    Location
    Muskegon, MI
    Posts
    2,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try -

    Select Count(DataCol) as ColumnCount, KeyCol
    From Table
    Group By KeyCol

    It should give you something like -
    25 A
    13 B
    10 C

    With a little server-side code you should be able to calculate percentages.
    Westmich
    Smart Web Solutions for Smart Clients
    http://www.mindscapecreative.com


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
  •