\n
PostgreSQL cheat sheet
\n Time series:\n
\n - \n return column named time (UTC in seconds or timestamp)\n
\n - return column(s) with numeric datatype as values
\n
\n Optional:\n
\n - \n return column named metric to represent the series name.\n
\n - If multiple value columns are returned the metric column is used as prefix.
\n - If no column named metric is found the column name of the value column is used as series name
\n
\n
Resultsets of time series queries need to be sorted by time.
\n Table:\n
\n - return any set of columns
\n
\n Macros:\n
\n - $__time(column) -> column as "time"
\n - $__timeEpoch -> extract(epoch from column) as "time"
\n - \n $__timeFilter(column) -> column BETWEEN '2017-04-21T05:01:17Z' AND\n '2017-04-21T05:01:17Z'\n
\n - $__unixEpochFilter(column) -> column >= 1492750877 AND column <= 1492750877
\n - \n $__unixEpochNanoFilter(column) -> column >= 1494410783152415214 AND column <= 1494497183142514872\n
\n - \n $__timeGroup(column,'5m'[, fillvalue]) -> (extract(epoch from column)/300)::bigint*300 by setting\n fillvalue grafana will fill in missing values according to the interval fillvalue can be either a literal\n value, NULL or previous; previous will fill in the previous seen value or NULL if none has been seen yet\n
\n - \n $__timeGroupAlias(column,'5m') -> (extract(epoch from column)/300)::bigint*300 AS\n "time"\n
\n - $__unixEpochGroup(column,'5m') -> floor(column/300)*300
\n - $__unixEpochGroupAlias(column,'5m') -> floor(column/300)*300 AS "time"
\n
\n
Example of group by and order by with $__timeGroup:
\n
\n \n SELECT $__timeGroup(date_time_col, '1h'), sum(value) as value
\n FROM yourtable\n
\n GROUP BY time\n
\n ORDER BY time\n
\n
\n
\n Or build your own conditionals using these macros which just return the values:\n
\n - $__timeFrom() -> '2017-04-21T05:01:17Z'
\n - $__timeTo() -> '2017-04-21T05:01:17Z'
\n - $__unixEpochFrom() -> 1492750877
\n - $__unixEpochTo() -> 1492750877
\n - $__unixEpochNanoFrom() -> 1494410783152415214
\n - $__unixEpochNanoTo() -> 1494497183142514872
\n
\n
\n );\n}\n\nfunction getStyles(theme: GrafanaTheme2) {\n return {\n ulPadding: css({\n margin: theme.spacing(1, 0),\n paddingLeft: theme.spacing(5),\n }),\n };\n}\n","import React from 'react';\n\nimport { QueryEditorProps } from '@grafana/data';\nimport { SqlQueryEditor, SQLOptions, SQLQuery, QueryHeaderProps } from '@grafana/sql';\n\nimport { PostgresDatasource } from './datasource';\n\nconst queryHeaderProps: Pick