SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    query help needed - count occurrences per hour

    I am trying to create a query that will count the number of times
    each value in field/incident occurs in an hour.

    Sample Data:
    Date_occurred Time_occurred incident
    | 2011-03-01 | 08:25:00 | RCCD |
    | 2011-03-01 | 08:22:00 | RCCDSAC |
    | 2011-03-01 | 08:24:00 | RCCDSAC |
    | 2011-03-01 | 08:25:00 | RCCDSAC |
    | 2011-03-01 | 08:08:00 | RCCD |
    | 2011-03-01 | 08:11:00 | RCCD |
    | 2011-03-01 | 08:05:00 | RCCD |
    | 2011-03-01 | 08:03:00 | RCCDIM |
    | 2011-03-01 | 07:09:00 | RCCDEM |
    | 2011-03-01 | 06:29:00 | RCCDIM |
    | 2011-03-01 | 06:32:00 | RCCDEM |
    | 2011-03-01 | 06:25:00 | RCCDEM |
    | 2011-03-01 | 06:16:00 | RCCDEM |
    | 2011-03-01 | 05:59:00 | RCCDSAC |
    | 2011-03-01 | 06:01:00 | RCCDSAC |
    | 2011-03-01 | 06:02:00 | RCCDSAC |
    | 2011-03-01 | 06:04:00 | RCCDSAC |
    | 2011-03-01 | 06:05:00 | RCCDEM |
    | 2011-03-01 | 05:57:00 | RCCDEM |

    What I would like the result to look like this:

    Hour RCCD# RCCDSAC# RCCDIM# RCCDEM#
    08 | 4 | 3 | 1 | 0
    07 | 0 | 0 | 0 | 1
    06 | 3 | 0 | 1 | 2


    I think that I understand how to group by hour but Iím not sure how to get the totals for each incident type for the hour

    Any help will be appreciated.

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Extract the totals for each incident type like this:

    hour incident total

    and then create the horizontal layout using PHP (or whatever language you use).

  3. #3
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004 View Post
    Extract the totals for each incident type like this:

    hour incident total

    and then create the horizontal layout using PHP (or whatever language you use).
    Thank you for your reply

    My goal is to get this data for a 24 hour period with a single query. I don't want to have to query each hour for each incident type.

    I do similar queries with other data, not datetime fields, using sum(if()) statements but have not been able to figure out how to do this when grouping by hour.

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,500
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Code:
    SELECT 
        HOUR(Time_occurred) AS hourofday
      , incident
      , COUNT(*)
    FROM table
    WHERE Date_occurred = ...
    GROUP BY 
        HOUR(Time_occurred)
      , incident


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
  •