group by minutes help

  • I have an audit log table and I need to display the results grouped by the minute of insert of event. I use datepart, but then it groups them wrong as there are inserts for more then 1 day, when I add a day then it groups them by seconds. What I'm doing wrong?

    select( datepart(mm,rcv_dt)+datepart(dd,rcv_dt)+ datepart(hh,rcv_dt)+datepart(mi,rcv_dt)) AS MINUTE,count (*) AS ERPM

    from audit_LOG1

    group by (datepart(mm,rcv_dt)+datepart(dd,rcv_dt)+ datepart(hh,rcv_dt)+datepart(mi,rcv_dt))

    ORDER BY 1 ASC;

    Thanks for the help.MJ

  • select convert(varchar(14),rcv_dt,20) AS MINUTE,count (*) AS ERPM

    from audit_LOG1

    group by  convert(varchar(14),rcv_dt,20)

    ORDER BY 1


    * Noel

  • ORDER BY 1 is not gonna be supported in Yukon... just to make sure you knew about this .

  • I have tried that in Yukon, Apparently you just  saved me time

     


    * Noel

  • If ORDER BY 1 is not supported, what replaced it?  Specifying the column, (or in this case, the display) data directly?  

    I wasn't born stupid - I had to study.

  • Thanks a lot, guys, you saved me so much time.

  • OK, but this doesn't work the way I need it:

    it groups by seconds not by minutes...

    How can I make it group by minutes?

    Thanks,mj

  • Oops its 16

    select convert(varchar(16),rcv_dt,20) AS MINUTE,count (*) AS ERPM

    from audit_LOG1

    group by  convert(varchar(16),rcv_dt,20)

    ORDER BY 1

     


    * Noel

  • Good thing you tested those results Noeld .

  • If you use CAST(rcv_dt as smalldatetime) it will group by minutes without resorting to string manipulation.

  • it's a good idea but it doesn't seem to work :

    Select

    cast('2005-05-02 14:56:52.493' as smalldatetime)

    , cast ('2005-05-02 14:57:03.447' as smalldatetime)

    , convert(varchar(16),'2005-05-02 14:56:52.493',20)

    , convert(varchar(16),'2005-05-02 14:57:03.447',20)

    2005-05-02 14:57:00

    2005-05-02 14:57:00

    2005-05-02 14:56

    2005-05-02 14:57

  • It rounds instead of truncates.  You can make it work the way you want by subtracting 30 seconds.

    Select

    cast(dateadd(s,-30,'2005-05-02 14:56:52.493') as smalldatetime)

    , cast (dateadd(s,-30,'2005-05-02 14:57:03.447') as smalldatetime)

    , convert(varchar(16),'2005-05-02 14:56:52.493',20)

    , convert(varchar(16),'2005-05-02 14:57:03.447',20)

    2005-05-02 14:56:00

    2005-05-02 14:57:00

    2005-05-02 14:56

    2005-05-02 14:57

  • Thanx. Looks like an index seek is just out of the question here, so the simplest conversion wins .

Viewing 13 posts - 1 through 12 (of 12 total)

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