Skip to content

Part 3: Working with Time

References: - ClickHouse date and time functions - ClickHouse formatDateTime

One important property of the telemetry that SenseOn receives is the time at which the events occurred. You used this time in the earlier parts, with the _time_observed >= toUnixTimestamp(subtractHours(now(), 24)) * 1000 filter. There are two different columns for the time: _time_observed and _time_ingested. In this exercise, use only the observed times.

The time values are in Unix epoch format. The interface converts them in the background and shows a human-readable time in your time zone. To get the Unix value for a time, you can use an online epoch converter. You can also click the time field in a result to copy that value to the clipboard.

This method can be inconvenient. Also, in an investigation you often want to look a little further back in time for context. With Unix epoch time, you can add to or subtract from the copied value. Use a - or +, followed by the number of milliseconds that you want to add or subtract:

1000 * 60                = 1 minute (1000 milliseconds * 60 seconds)
1000 * 60 * 60           = 1 hour
1000 * 60 * 60 * 24      = 1 day
1000 * 60 * 60 * 24 * 2  = 2 days

3a. Unix epoch time

Write a query that shows all columns from the endpoint_process table. Change the _time_observed >= toUnixTimestamp(subtractHours(now(), 1)) * 1000 line to limit the search to 12 hours. Return the oldest 50 results. Sort them from oldest to newest. Is the first record 12 hours before the current time?

Answer
SELECT *
FROM endpoint_process
WHERE
  _time_observed >= toUnixTimestamp(subtractHours(now(), 12)) * 1000
ORDER BY _time_observed asc
LIMIT 50

Change the time limit to 1 hour. Run the query again. Sort the results from oldest to newest. Click the _time_observed column to copy the epoch value to the clipboard.

Answer
SELECT *
FROM endpoint_process
WHERE
  _time_observed >= toUnixTimestamp(subtractHours(now(), 1)) * 1000
ORDER BY _time_observed asc
LIMIT 50
Copy the oldest epoch value from `_time_observed` to use in the next query.

Now change the previous query. Remove the _time_observed >= toUnixTimestamp(subtractHours(now(), 1)) * 1000 line. Make the query return all process records from the 5 minutes before the value that you copied. Sort them in ascending order of time. Is the first record in this output approximately 5 minutes before the oldest record from the previous query?

Hint - 5 minutes There are 300000 milliseconds in 5 minutes.
Answer
SELECT *
FROM endpoint_process
WHERE
  _time_observed >= 1773400919205 - 300000 -- replace this epoch value with the one you copied
ORDER BY _time_observed asc
LIMIT 50

3b. Typecasting time

Use toDateTime for some operations. Also use it to change time values into a human-friendly form, which makes timezone conversion easier. This function converts the Unix epoch value into a date time data type.

The structure is toDateTime(_time_observed / 1000, 'Continent/City') AS <meaningful_name>. Do the conversion in the SELECT statement. If you do not specify a timezone, the result shows the equivalent time in UTC. The / 1000 is important, because the epoch times here have millisecond precision, but toDateTime works in seconds.

💡 Efficiency: This method gives time constraints in a more human-friendly form. But it is less efficient than a filter on the raw epoch value with toUnixTimestamp(subtractHours(now(), 1)) * 1000. Also, it is necessary to know the timezone that you convert to, and how it compares with the timezone that the interface shows.

Write a query that converts the time to UTC in the SELECT clause. Use the 50 most recent entries in the endpoint_process table. Use an AS statement to label the column as UTC time. The query must show the converted time, _hostname, name and parent_name. To make the query run efficiently, continue to use toUnixTimestamp(subtractHours(now(), <hours to subtract>)) * 1000 as a limit. Notice that the format of the converted column is different from the format of the usual _time_observed column.

Answer
SELECT
  toDateTime(_time_observed / 1000, 'UTC') AS UTCTime,
  _hostname,
  name,
  parent_name
FROM endpoint_process
WHERE
  _time_observed >= toUnixTimestamp(subtractHours(now(), 1)) * 1000
ORDER BY _time_observed desc
LIMIT 50

Now change the query so that it shows the time in your own timezone. Does the most recent record match your current time?

Answer
SELECT
  toDateTime(_time_observed / 1000, 'Europe/London') AS MyTime, -- use your own timezone here
  _hostname,
  name,
  parent_name
FROM endpoint_process
WHERE
  _time_observed >= toUnixTimestamp(subtractHours(now(), 1)) * 1000
ORDER BY _time_observed desc
LIMIT 50

3c. Working with dateTime

After you convert the time to a date time data type, you can use a more human-friendly syntax. Use it in the WHERE clause to specify the time ranges. The format is YYYY-MM-DD HH:MM:SS, on a 24-hour clock:

MyTime >= '2026-04-15 09:30:00'

💡 The alias of your new column: ClickHouse lets you use an alias from the SELECT clause in the WHERE clause. Thus, after you write AS MyTime, you can filter directly on MyTime. You do not have to write the full toDateTime(...) expression again.

Remove the toUnixTimestamp(subtractHours(now(), 1)) * 1000 line from the previous query. In its place, use the date time syntax to limit the results to approximately the previous 3 hours. Use your own timezone. Sort the results from oldest to newest. Are the first entries approximately 3 hours before your current time?

Answer
SELECT
  toDateTime(_time_observed / 1000, 'Europe/London') AS MyTime, -- use your own timezone here
  _hostname,
  name,
  parent_name
FROM endpoint_process
WHERE
  MyTime >= '2026-04-15 09:00:00' -- replace with a time approximately 3 hours ago
ORDER BY MyTime asc
LIMIT 50
Your times are different from the times in the example. Thus, compare the output with the clock, not with a fixed value.

Change the query so that it sorts the newest results first. Add a second condition so that the newest results are from one hour ago. Are the results now one hour behind your current time?

Answer
SELECT
  toDateTime(_time_observed / 1000, 'Europe/London') AS MyTime, -- use your own timezone here
  _hostname,
  name,
  parent_name
FROM endpoint_process
WHERE
  MyTime >= '2026-04-15 09:00:00' -- approximately 3 hours ago
  AND MyTime <= '2026-04-15 11:00:00' -- approximately 1 hour ago
ORDER BY MyTime desc
LIMIT 50

Write a query that returns _time_observed, _hostname, name, parent_name and command from the endpoint_process table. Limit it to 50 results from the past 3 days. Typecast _time_observed to a date time. Then show only the date on which the event occurred. It is not necessary to sort the results. Do not use toDate for this. Use the format function in its place.

Hint - which function `formatDateTime` takes a date time value and a format string. It returns only the parts that you ask for. For a list of the format specifiers, refer to the ClickHouse documentation at the top of this page.
Answer
SELECT
  formatDateTime(toDateTime(_time_observed / 1000, 'Europe/London'), '%D') AS EventDate,
  _hostname,
  name,
  parent_name,
  command
FROM endpoint_process
WHERE
  _time_observed >= toUnixTimestamp(subtractDays(now(), 3)) * 1000
LIMIT 50
`%D` returns the date as `MM/DD/YY`. Any format with only the date is acceptable here. The next question asks for a specific format.

Change the previous query so that the output shows the date in this format: 2026-04-15.

Hint - format specifier One specifier gives the full ISO date. Thus, it is not necessary to combine `%Y`, `%m` and `%d` yourself.
Answer
SELECT
  formatDateTime(toDateTime(_time_observed / 1000, 'Europe/London'), '%F') AS EventDate,
  _hostname,
  name,
  parent_name,
  command
FROM endpoint_process
WHERE
  _time_observed >= toUnixTimestamp(subtractDays(now(), 3)) * 1000
LIMIT 50

Congratulations. You completed the beginner level Hunt Lab questions. Now you can:

  • Structure queries.
  • Filter queries.
  • Work with strings.
  • Use time in epoch form and in human-readable form.

The next level is the intermediate level. From this point, the questions give fewer clues. You must often find the necessary columns from the shape of the expected result, not from the question.

Now you can continue to Part 4 of the Hunt Lab training.