Time rounding to different intervals

790 views
Skip to first unread message

Saptarshi Gan

unread,
Dec 4, 2017, 9:31:17 AM12/4/17
to ClickHouse
Like toStartOfMinute toStartOfHour is it possible to round up the date time to any random interval? Like to Start of 2 minute or to Start of 10 minute!

Saptarshi Gan

unread,
Dec 4, 2017, 9:44:14 AM12/4/17
to ClickHouse
Is there a way to convert datetime to unix timestamp?

Saptarshi Gan

unread,
Dec 4, 2017, 10:17:03 AM12/4/17
to ClickHouse
Multiple CASTS would do the trick I guess!

CAST(floor(CAST(timestamp AS UInt64)/300)*300 AS DateTime)

Alex Zatelepin

unread,
Dec 4, 2017, 11:29:56 AM12/4/17
to ClickHouse
You can use functions toUnixTimestamp and intDiv for this task - that will be slightly more efficient. Your query becomes:

toDateTime(intDiv(toUnixTimestamp(timestamp), 300) * 300)

(BTW there is a function toStartOfFiveMinute specifically for 300 seconds)
Reply all
Reply to author
Forward
0 new messages