Row over partition

  • I have a large query. I would like to count the number of instances in an hour.

    Is it possible to use row partition to achieve this?

    My table is as follows:

    VisitTimes

    00:20:00
    01:25:00
    01:30:00
    01:45:00
    02:01:00
    02:02:00
    02:05:00
    02:06:00
    03:10:00
    03:12:00
    03:13:00

    So the results would be
            
           midnight would be 1
             1am would be 3
      2am would be 4
      3am would be 3

  • dbman - Tuesday, June 26, 2018 8:00 AM

    I have a large query. I would like to count the number of instances in an hour.

    Is it possible to use row partition to achieve this?

    My table is as follows:

    VisitTimes

    00:20:00
    01:25:00
    01:30:00
    01:45:00
    02:01:00
    02:02:00
    02:05:00
    02:06:00
    03:10:00
    03:12:00
    03:13:00

    So the results would be
            
           midnight would be 1
             1am would be 3
      2am would be 4
      3am would be 3

    Just need to use GROUP BY on hour, no need for partition. If you include DDL & INSERT statements, someone will give you a working solution.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • As Phil mentioned, you can use Datepart and Group By to achieve this.

    DECLARE @t table (TimeValues time)
    INSERT INTO @t VALUES
    ('00:20:00'),
    ('01:25:00'),
    ('01:30:00'),
    ('01:45:00'),
    ('02:01:00'),
    ('02:02:00'),
    ('02:05:00'),
    ('02:06:00'),
    ('03:10:00'),
    ('03:12:00'),
    ('03:13:00')

    SELECT DATEPART(hh,Timevalues) AS HourOfDay,COUNT(DATEPART(hh,Timevalues)) AS Total
    FROM @t
    GROUP BY DATEPART(hh,Timevalues)

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

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