-1

We are using the graph compare time and we want to compare the amount of sessions for example between 14 days ago and 28 days ago and same wih 30 days ago and 60 days ago.

We currently wrote date BETWEEN $__timeFrom() AND $__timeTo() and it changes upon selecting the date ranges on the top right but we want the client to be able to change the date ranges and for this query to adjust to show -14 days and -30 days ago as well.

SELECT
  $__timeGroup(date, '24h'),
  sum(sessions) as sessions
FROM
  ourchart
WHERE
  date BETWEEN $__timeFrom() AND $__timeTo()
  group by time
  order by time

EDIT - I am able to make intervals of course but we are trying to compare this month to last month in the same graph....

  • What problem are you having specifically with running this query? Is the problem the calculation of the from/to dates? If so, how are you calculating them presently? – halfer Jan 25 '19 at 00:39
  • Hi! Thank you for your response! So our issue is that we want it to show from 14 days ago to 28 days ago but i am not sure how to write this for grafana! (our database is postgresql) – Daniela Shvartsman Jan 25 '19 at 00:44

1 Answers1

1

Play with interval math. Example, which will move your timerange 30 days back:

date BETWEEN 
  timestamp $__timeFrom() - interval '30 days' AND 
  timestamp $__timeTo() - interval '30 days'
Jan Garaj
  • 25,598
  • 3
  • 38
  • 59
  • I guess that one would be similar to my query - SELECT $__timeGroup(date, '24h'), sum(sessions) as sessions_30d FROM ourchart WHERE date BETWEEN (CURRENT_TIMESTAMP - INTERVAL '60 day') AND (CURRENT_TIMESTAMP - INTERVAL '30 day') GROUP BY date ORDER BY date But were trying to make it more dynamic based on users input :( Thank you so much though! @jan-garaj – Daniela Shvartsman Jan 25 '19 at 17:10