How do I group and sum all rows in between selected date range?

  • What Grafana version and what operating system are you using?
    Latest grafana standard version on centos8.

  • What are you trying to achieve?
    I have a table that displays revenue and expenses for every day. I want to calculate the net profit or each day and then sum all of the rows together. For example, if I make a date range selection of the previous 30 days then I would like to see the net profit for the previous 30 days as a single number.

  • How are you trying to achieve it?

SELECT date_trunc(‘day’, date) AS day,
SUM(revenue - spend_usd) AS Profit
FROM merged_table
WHERE date BETWEEN $__timeFrom() AND $__timeTo()

  • What happened?

This gives me the profit column for each date, but it doesn’t sum them together.

  • What did you expect to happen?

I expected the BETWEEN $__timeFrom() AND $__timeTo() to help achieve this.

maybe do

GROUP BY date_trunc(‘day’, date) 


group by 1