Results 1 to 3 of 3

Thread: Count the number of times the name

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2002
    Location
    Hampton Beach
    Posts
    513

    Question Count the number of times the name

    I have an access database setup as follows:

    Field1 Field2 Field3 Field4
    John Mike Mike Larry
    Steve Danny Mike John


    Using vb what is the query to display the data like this:

    Position Name How many

    1 Mike 3
    2 John 2
    3 Larry 1
    4 Steve 1
    5 Danny 1

  2. #2
    PowerPoster Beacon's Avatar
    Join Date
    Jan 2001
    Location
    Pub Floor
    Posts
    3,188
    In the top thread you have an FAQ on SQL statements use the Count function.

    E.g:

    SELECT Count(Field1) FROM table1 Where field1 = 'mike'

    Would be a simple way. Now using the distinct function in there would eliminate the need for the WHere statement.

    E.g:

    SELECT DISTINCT Count(field1) FROM table1

    good luck
    b

  3. #3
    Hyperactive Member
    Join Date
    Nov 1999
    Location
    Leavenworth KS USA
    Posts
    482
    You've a cruddy table design & really should fix it before you proceed, but this should help you on your way...

    Code:
    -- TSQL
    SELECT fName, COUNT(fName) AS HowMany FROM
    ( SELECT Field1 FROM FoolishTable
      UNION ALL
      SELECT Field2 FROM FoolishTable
      UNION ALL
      SELECT Field3 FROM FoolishTable
      UNION ALL
      SELECT Field4 FROM FoolishTable ) AS NeedDBA(fName)
    GROUP BY fName
    ORDER BY COUNT(fName) DESC
    
    -- MS Access
    SELECT Field1 AS fName, Count(Field1) AS HowMany FROM 
    ( SELECT Field1, 1 FROM FoolishTable
       UNION ALL
       SELECT Field2, 2 FROM FoolishTable
       UNION ALL
       SELECT Field3, 3 FROM FoolishTable
       UNION ALL
       SELECT Field4, 4 FROM FoolishTable )
    GROUP BY Field1
    ORDER BY COUNT(Field1) DESC;
    Last edited by Mongo; Jan 15th, 2003 at 05:46 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width