Get last 30 minutes of activity from table

  • I need to pull the last 30 minutes of activity from a table that has a field defined and stored in Unix format, which is a bigint field with the number of milliseconds since 01/01/1970.

    The field is called t_stamp  I want to get the last 30 minutes of activity from that table.

    Thanks.

     

  • For millisecond precision it would be something like,

    SELECT DATEDIFF_BIG(ms, '19700101' ,DATEADD(minute, -30, getdate()))

  • This gets me the date from the table:

    DATEADD(s,t_stamp/1000,'1970-01-01 00:00:00') ..  I want to extract data 30 minutes prior from current datetime using the expression from t.stamp field  ... maybe I asked wrong..

    Thanks.

  • If you calculate the unix millisecond timestamp 30 minutes ago all you should need to do is look for time stamps greater than that.

  • WHERE t_stamp >= DATEDIFF_BIG(MS, '19700101', GETDATE()) - (1000 * 60 * 30)

    Note that, for important performance reasons, you do not want to use any function at all on the column itself, only on the literal values / expressions.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • That looks correct to me, but I'm getting records with dates past even current date time. I'm checking on the process writing records to

    the table, but something is a miss ..

    Thanks.

  • I put no max time, so, yes, it would give you everything from then on.

    If you want to limit to the then-current time, do this:

    WHERE t_stamp >= DATEDIFF_BIG(MS, '19700101', GETDATE()) - (1000 * 60 * 30)

    AND t_stamp <= DATEDIFF_BIG(MS, '19700101', GETDATE())

     

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • I just pulled one of the latest records written to table and when I convert it it shows 2021-06-15 19:53:37.000

    THanks.

  • Hmm, looks like UTC time.

    Or something in the data -- i.e. it's not really ms since 1/1/70 -- or it's local time where the server is?!

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • I was thinking UTC  if I use your query that sets an EndTime data looks correct..

  • To get a UTC calc, just substitute GETUTCDATE() for GETDATE() in my code.

    SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".

  • I'm checking now the data

    Thanks.

  • Found that I had to use '1969-12-31 20:00:00' to get the correct datetime. So I'm looking for the most efficient way to pull the last 30 minutes of data from the table. This converts it to date\time I want to use DATEADD(s,t_stamp/1000,'1969-12-31 20:00:00'), but should I use t_stamp in it's field def as a BigInt as a  way to go back to pull the date.

    Thanks.

     

  • Once this is converted to a time, this should be easy... get a MAX of the time, and then subtract 30 mins, so something like  [theTime]>=(SELECT DATEADD(minute,-30,MAX(Time))

  • Not sure I'm following convert to time

    Thanks,

Viewing 15 posts - 1 through 15 (of 17 total)

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