SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select and group based upon timestamp

    I have a table containing view/click records. The time is stored in a unix timestamp and I need to be able to pull out all of them within the specific month/day (based off of timestamps), but more importantly and the part I don't know how to do is group them by hour. I need to be able to do this in a single query rather than looping through each hour.

    Database is MySQL, language is PHP.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT COUNT(*) cnt
         , EXTRACT(HOUR FROM FROM_UNIXTIME(created_on)) hr
      FROM user_login_log
     WHERE created_on >= UNIX_TIMESTAMP(DATE('2009-10-27'))
       AND created_on < UNIX_TIMESTAMP(DATE('2009-10-27') + INTERVAL 1 DAY)
    GROUP
        BY hr

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,022
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    awesome SQL formatting

    leading comma convention, too

    just awesome
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ya you should copy it imo

  5. #5
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is why I came to sitepoint.... awesome code... thanks

  6. #6
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    SELECT COUNT(*)cnt, EXTRACT(HOUR FROM FROM_UNIXTIME(timestamp)) hr FROM ad_view WHERE timestamp > '.$start.' AND timestamp < '.$end.' GROUP BY hr
    The code returns:

    Code:
    Array
    (
        [cnt] => 5
        [hr] => 0
    )
    Can you explain more of how the interval works so I can adapt it to what I'm working with?

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,022
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    please give a few sample rows of data
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    SitePoint Guru
    Join Date
    Sep 2004
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I consulted another friend and got my solution. Thanks for your assistance though

    Code:
    SELECT
    		COUNT(id) as view_count,
    		DAY(FROM_UNIXTIME(timestamp)) as day
    	FROM ad_view WHERE timestamp > '.$start.' AND timestamp < '.$end.' GROUP BY day


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
  •