integral()

integral(
tws TimeWeightSummary,
unit TEXT
) RETURNS DOUBLE PRECISION

Compute a time-weighted integral from a TimeWeightSummary.

This function is similar to average but doesn't divide by the length of time being integrated.

Required arguments

NameTypeDescription
twsTimeWeightSummaryThe input TimeWeightSummary from a time_weight call

Optional arguments

NameTypeDescription
unitTEXTThe unit of time to express the integral in. Can be microsecond/millisecond/second/minute/hour or any alias for those units supported by PostgreSQL. If NULL, defaults to second.

Returns

ColumnTypeDescription
integralDOUBLE PRECISIONThe time-weighted integral computed from the TimeWeightSummary

Sample usage

-- Create a table to track irregularly sampled storage usage
CREATE TABLE user_storage_usage(ts TIMESTAMP, storage_bytes BIGINT);
INSERT INTO user_storage_usage(ts, storage_bytes) VALUES
('01-01-2022 00:00', 0),
('01-01-2022 00:30', 100),
('01-01-2022 03:00', 300),
('01-01-2022 03:10', 1000),
('01-01-2022 03:25', 817);
-- Get the total byte-hours used
SELECT
time_weight('LOCF', ts, storage_bytes) ->
toolkit_experimental.integral('hours')
FROM
user_storage_usage;

Found an issue on this page?

Report an issue!

Keywords

Related Content