21.4. Time-Based Data Analysis #

This section describes how to analyze time-based data.

21.4.1. Grouping Timestamps into Time Intervals (time_bucket) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT time_bucket(INTERVAL 'time_interval', timestamp_column [, alignment_timestamp]);

Where:

  • time_interval: The time interval for grouping timestamps, e.g., 1 hour or 15 minutes.

  • timestamp_column: The column with timestamps that must be grouped into time intervals.

  • alignment_timestamp: The timestamp that determines the starting point for time intervals. By default, intervals start at the Unix epoch, so the first interval starts at 1970-01-01 00:00:00 UTC.

    Optional parameter.

Postgres Pro AXE will return the starting point of the time interval for each timestamp as a TIMESTAMP value.

Example 21.27. Grouping Timestamps into Time Intervals

Grouping events by hour:

  SELECT time_bucket(INTERVAL '1 hour', created_at) AS hour_bucket, COUNT(*)
  FROM events
  GROUP BY hour_bucket
  ORDER BY hour_bucket;

Grouping sensor data by 15-minute time intervals:

  SELECT time_bucket(INTERVAL '15 minutes', timestamp_col), AVG(value)
  FROM sensor_data
  WHERE timestamp_col >= '2024-01-01'
  GROUP BY 1
  ORDER BY 1;

21.4.2. Converting a Timestamp to a String (strftime) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT strftime(timestamp, 'format');

Where:

  • timestamp: The timestamp that must be converted to a string.

  • format: The format of the timestamp.

    Possible values:

    • %Y: 4-digit year (2024).

    • %m: Month as a number (01-12).

    • %d: Day of the month (01-31).

    • %H: Hour (00-23).

    • %M: Minute (00-59).

    • %S: Second (00-59).

    • %A: Full weekday name (Monday).

    • %B: Full month name (January).

    • %b: Abbreviated month name (Jan).

Postgres Pro AXE will return the converted timestamp as a TEXT value.

Example 21.28. Converting a Timestamp to a String

Converting the current timestamp to a string:

  SELECT strftime(NOW(), '%Y-%m-%d %H:%M:%S') AS formatted_time;

Converting timestamps from the orders table:

  SELECT
      order_id,
      strftime(created_at, '%Y-%m-%d') AS order_date,
      strftime(created_at, '%H:%M') AS order_time,
      strftime(created_at, '%A, %B %d, %Y') AS readable_date
  FROM orders;

21.4.3. Converting a String to a Timestamp (strptime) #

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT strptime('string', 'format');

Where:

  • string: The string that must be converted to a timestamp.

  • format: The format of the string.

    Possible values:

    • %Y: 4-digit year (2024).

    • %m: Month as a number (01-12).

    • %d: Day of the month (01-31).

    • %H: Hour (00-23).

    • %M: Minute (00-59).

    • %S: Second (00-59).

    • %A: Full weekday name (Monday).

    • %B: Full month name (January).

    • %b: Abbreviated month name (Jan).

Postgres Pro AXE will return the converted string as a TIMESTAMP value.

Example 21.29. Converting a String to a Timestamp

Converting date strings in different formats to timestamps:

  SELECT strptime('Jan 15, 2024', '%b %d, %Y') AS date1,
      strptime('15/01/2024', '%d/%m/%Y') AS date2,
      strptime('2024-01-15T14:30:00Z', '%Y-%m-%dT%H:%M:%SZ') AS iso_date;

Converting date strings from the raw_logs table:

  SELECT
      log_id,
      strptime(timestamp_string, '%Y-%m-%d %H:%M:%S') AS parsed_time,
      message
  FROM raw_logs;

21.4.4. Creating a Timestamp from Microseconds (make_timestamp) #

The make_timestamp stored procedure creates a timestamp from the number of microseconds since the Unix epoch. The Unix epoch is 1970-01-01 00:00:00 UTC.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT make_timestamp(microseconds);

Where microseconds is the number of microseconds since the Unix epoch.

Postgres Pro AXE will return the created timestamp as a TIMESTAMP value.

Example 21.30. Creating a Timestamp from Microseconds

  SELECT make_timestamp(1640995200000000) AS new_years_2022;

21.4.5. Creating a Timestamp with Time Zone from Microseconds (make_timestamptz) #

The make_timestamptz stored procedure creates a timestamp with time zone from the number of microseconds since the Unix epoch. The Unix epoch is 1970-01-01 00:00:00 UTC.

Required privileges: Superuser or the Postgres Pro AXE administrator.

For more information about access management, refer to Chapter 12.

Execute the following command on the Postgres Pro AXE server:

  SELECT make_timestamptz(microseconds);

Where microseconds is the number of microseconds since the Unix epoch.

Postgres Pro AXE will return the created timestamp with time zone as a TIMESTAMPTZ value.

Example 21.31. Creating a Timestamp with Time Zone from Microseconds

  SELECT make_timestamptz(1640995200000000) AS new_years_2022_tz;

FAQ