Search code examples
postgresqlgrafanadashboard

Grafana charts grouped by day or month?


I am stuck with the graph due to not being able to group the graphs by date. I am using dates and counters to make my queries visual, the problem is that adding them to grafana looks like this.

My problem:

enter image description here

Query:

select 
   to_char(created,'yyyy-mm-dd') as "time", count(created) as created
from
   [table]
group by
   to_char(created,'yyyy-mm-dd') 

I use a similar query for each graph, coming to be visualized as My problem image.

enter image description here

How can I make it look like this?


Solution

  • Use some $__timeGroup*/$__unixEpochGroup* macro (it depends on the column created type) - https://grafana.com/docs/grafana/latest/datasources/postgres/#macros. Example:

    SELECT
      $__unixEpochGroupAlias(created, 30d),
      count(*) AS "count"
    FROM $table
    WHERE
      $__unixEpochFilter(created)
    GROUP BY 1
    ORDER BY 1
    

    Eventually you need to write SQL on your own to group by calendar month.