# Aggregate Functions

Warning! This page documents an earlier version of InfluxDB, which is no longer actively developed. InfluxDB v1.7 is the most recent stable version of InfluxDB.

InfluxDB contains a number of functions that you can use for computing aggregates, rollups, or doing downsampling on the fly. These are always used in conjunction with a `group by time(...)` clause. If no group by clause is given, then a default will be applied from the start of the series to `now()`.

## Count

COUNT() takes a single column name, and counts the number of points that contains a non NULL value for the given column name in the group by interval.

``SELECT COUNT(column_name) FROM series_name group by time(10m) ...``

## Min

MIN() returns the lowest value from the specified column over a given interval. The column must contain int64 or float64 values.

``SELECT MIN(column_name) FROM series_name group by time(10m) ...``

## Max

MAX() returns the highest value from the specified column over a given interval. The column must be of type int64 or float64.

``SELECT MAX(column_name) FROM series_name group by time(10m) ...``

## Mean

MEAN() returns the arithmetic mean (average) of the specified column over a given interval. The column must be of type int64 or float64.

``SELECT MEAN(column_name) FROM series_name group by time(10m) ...``

## Mode

MODE() returns the most frequent value(s) of the specified column over a given interval. The column must be of type int64 or float64. Since a timeseries can be multimodal (contain multiple values that occur the same number of times), this can potentially return multiple rows.

``SELECT MODE(column_name) FROM series_name group by time(10m) ...``

## Median

MEDIAN() returns the middle value from a sorted set of values for the specified column over a given interval. This is nearly equivalent to PERCENTILE(column_name, 50), except that in the event a dataset contains an even number of points, the median will be the average of the two middle values.

``SELECT MEDIAN(column_name) FROM series_name group by time(10m) ...``

## Distinct

DISTINCT() returns unique values for the given column.

``SELECT DISTINCT(column_name) FROM series_name group by time(10m) ...``

You can nest distinct in count to get the counts of unique values over windows of time:

``````SELECT COUNT(DISTINCT(column_name)) from series_name
group by time(10m) ...``````

## Percentile

PERCENTILE() returns the Nth percentile of a sorted set of values for the specified column over a given interval. PERCENTILE() requires two values, the second of which can be either an integer or floating point number between 0 and 100.

``SELECT PERCENTILE(column_name, N) FROM series_name group by time(10m) ...``

## Histogram

HISTOGRAM() requires at least one argument and at most two arguments. The first argument is the column name and the second argument is the bucket size. Bucket size defaults to `1.0` if it wasn’t specified. HISTOGRAM() outputs two columns `bucket_start` and `count`. `bucket_start` is the smallest value in the bucket. `bucket_start` along with the bucket size defines the current bucket. `count` is the number of points that falls in this bucket.

``````SELECT HISTOGRAM(column_name) FROM series_name ...

SELECT HISTOGRAM(column_name, 10.0) FROM series_name ...``````

## Derivative

DERIVATIVE() requires exactly one argument, which is a column name. The out is a column containing the value of ```(v_last - v_first) / (t_last - t_first)``` where `v_last` is the last value of the given column and `t_last` is the corresponding timestamp (and similarly for `v_first` and `t_first`). In other words, DERIVATIVE() calculates the rate of change of the given column.

``SELECT DERIVATIVE(column_name) FROM series_name ...``

## Sum

SUM() requires exactly one argument, which is a column name. It outputs the sum of the all values for the given column.

``SELECT SUM(column_name) FROM series_name ...``

## Stddev

STDDEV() requires exactly one argument, which is a column name. It outputs the standard deviation of the given column.

``SELECT STDDEV(column_name) FROM series_name ...``

## First/Last

FIRST() and LAST() require exactly one argument, which is a column name. It will output the first or last point for each group by interval.

``````SELECT FIRST(column_name) FROM series_name ...

SELECT LAST(column_name) FROM series_name ...``````

## Difference

DIFFERENCE() requires one argument, which is a column name. It will output the difference in the first and last value for each group by interval.

``SELECT DIFFERENCE(column_name) FROM series_name ...``

## Top / Bottom

TOP() and BOTTOM require two arguments, the column name and the number of top results to return. It will output the top or bottom values for each group by interval.

``````SELECT TOP(column_name, N) FROM series_name ...

SELECT BOTTOM(column_name, N) FROM series_name ...``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.70725536,"math_prob":0.9589521,"size":4512,"snap":"2019-43-2019-47","text_gpt3_token_len":1013,"char_repetition_ratio":0.19853593,"word_repetition_ratio":0.1951567,"special_character_ratio":0.23071809,"punctuation_ratio":0.14473684,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9903745,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-11-18T08:21:45Z\",\"WARC-Record-ID\":\"<urn:uuid:ee135ab9-b529-46bf-8dfc-b6d256c6ead3>\",\"Content-Length\":\"33654\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:29f31a6a-c952-4e58-b10f-f21501416b98>\",\"WARC-Concurrent-To\":\"<urn:uuid:9615fa6f-2754-4590-8e50-6636e3d4cabc>\",\"WARC-IP-Address\":\"99.84.181.80\",\"WARC-Target-URI\":\"https://docs.influxdata.com/influxdb/v0.6/api/aggregate_functions/\",\"WARC-Payload-Digest\":\"sha1:S5JJEAWPFJTDRRUIU6MQ2IXJFYVF3DKP\",\"WARC-Block-Digest\":\"sha1:PXKJBZVY55VCCMAA7XLS3WV2TW22FX3B\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-47/CC-MAIN-2019-47_segments_1573496669730.38_warc_CC-MAIN-20191118080848-20191118104848-00484.warc.gz\"}"}