Group results by the hour?

  • Newbie needed help...

    I have a datestamp in my database that goes to the milisecond. I need group results by the hour. I've tried the following but that just hides the results rather than grouping them.

    select left(calltime,10), count(id) as total from table1

    group by calltime

    What I need for an output would be this:

    9a-10a XXXXX

    10a-11a XXXXX

    And so on, please help!

  • create table #tmp (id int identity, mydate datetime)

    insert into #tmp (mydate) values (getDate())

    select datepart(hh, mydate), count(*)

    FROM #tmp

    group by datepart(hh, mydate)

  • Are you all set with this or do you still need some help?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply