Results 1 to 7 of 7

Thread: SQL??

  1. #1

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565

    Question

    I have a table called Work_History and in it a field called Work_Date.

    How can I get hold of the Work_Date which occurs most often?

  2. #2
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    SELECT COUNT(Work_Date) AS Total, Work_Date FROM
    Work_History GROUP BY Work_Date
    ORDER BY Total DESC

    - the most common work_date would appear at the top of the returned recordset.

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  3. #3

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    When I use that SQL statement, it complains and seems to be waiting for me to pass in a value for Total.

    Any ideas?

  4. #4
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Just do

    SELECT COUNT(Work_Date), Work_Date
    FROM Work_History GROUP BY Work_Date
    ORDER BY COUNT(Work_Date) DESC

    The Total bit was just to make it look tidier.

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  5. #5

    Thread Starter
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565

    Thumbs up

    Cheers, works a treat.

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Lightbulb Select TOP n record

    You also use the SELECT...TOP n sql statement as well to perform you task.
    Where n is occurs most offen Work_Date in your databse. Exmaple, over here I put 5 that mean I wish to retrieve the top5 Work_Date that occurs most offen.

    Code:
    SELECT TOP 5 Work_Date FROM Work_History WHERE Work_Date <> "";

  7. #7
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    TOP [x] doesn't work in all flavours of SQL. If only it did!

    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

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