# Aggregate/Group Data in Grafana with SQL

**URL:** <https://community.influxdata.com/t/aggregate-group-data-in-grafana-with-sql/58717>\
**Category:** InfluxDB 3\
**Tags:** grafana, time, query, sql\
**Created:** [August 31, 2026, 6:57am UTC](https://community.influxdata.com/t/aggregate-group-data-in-grafana-with-sql/58717 "2026-08-31T06:57:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dominik.w](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@dominik.w](https://community.influxdata.com/u/dominik.w)\
**Post date:** [August 31, 2026, 6:57am UTC](https://community.influxdata.com/t/aggregate-group-data-in-grafana-with-sql/58717/1 "2026-08-31T06:57:09Z")

</div>

Hi,

I am using Influxdb3 to Store data from my Data logger.

Writing to it with the following line Protocol `spine,point=a_voltage,unit=V value=230.5 171234567890123456` works fine.

Currently I write multiple points every 250ms but the query in Grafana “sql” gets more heave over time.

Using following Query:

```sql
SELECT time AS "Time", value AS "A Phase Voltage"

FROM spine

WHERE point = 'a_voltage' AND time >= $__timeFrom() AND time <= $__timeTo()

```

Returns me every every stored point (250ms) which is fine for a short time period but Displaying like 24h gets difficult.

How can I use a custom variable or the Global Grafana `$__interval` one to Group/Aggregate Data to query more efficiently?

Hard-coding kinda works, but defeats the purpose of having such good resolution. And just using the variable instead of 1s does not work.

```sql
SELECT
  DATE_BIN(INTERVAL '1s', time, '1970-01-01T00:00:00Z') AS "Time",
  AVG(value) AS "A Phase Voltage 1s Interval"
FROM spine
WHERE point = 'a_voltage' AND time >= $__timeFrom() AND time <= $__timeTo()
GROUP BY 1

```

Thanks in advanced for the help!

---

<div class="post-metadata">

**Author:** ![cbowden](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/cbowden/32/20322_2.png) [@cbowden](https://community.influxdata.com/u/cbowden)\
**Post date:** [September 1, 2026, 5:50pm UTC](https://community.influxdata.com/t/aggregate-group-data-in-grafana-with-sql/58717/2 "2026-09-01T17:50:25Z")

</div>

Two thoughts:

Your approach here _should_ work, and it’s curious that you say using a variable does not work. You can define and use multiple variables in Grafana, so you’d just need a second variable where you define and set your date-binned interval. If you create a variable called `resolution`, give it the possible values of `250ms, 1s, 10s, 1m, 5m`, you can then modify your query to:

`SELECT`  
` DATE_BIN(INTERVAL '${resolution:raw}', time, '1970-01-01T00:00:00Z') AS "Time",`  
` AVG(value) AS "A Phase Voltage 1s Interval"`  
`FROM spine`  
`WHERE point = 'a_voltage' AND time >= $__timeFrom() AND time <= $__timeTo()`  
`GROUP BY 1`

You also don’t need to do this, though. The InfluxDB connector for Grafana comes with [a number of built-in macros](https://grafana.com/docs/grafana/latest/datasources/influxdb/query-editor/?utm_source=chatgpt.com#macros), and Grafana auto-determines a reasonable `$ __interval` macro based on your selected time range, which you can use in the `$__ dateBin` function without needing any extra variables.

If you were to use that, you could just use:

```auto
SELECT
  $__dateBin(time) AS "Time",
  AVG(value) AS "A Phase Voltage"
FROM spine
WHERE point = 'a_voltage' AND $__timeFilter(time)
GROUP BY 1;

```

And you’d only need to use a custom variable if the default interval/date-binning wasn’t to your liking.

---

<div class="post-metadata">

**Author:** ![dominik.w](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@dominik.w](https://community.influxdata.com/u/dominik.w)\
**Post date:** [September 2, 2026, 7:11am UTC](https://community.influxdata.com/t/aggregate-group-data-in-grafana-with-sql/58717/3 "2026-09-02T07:11:40Z")

</div>

Thanks for you answer! I appreciate your help.  
Its probably because I’m quite new to Influxdb and Grafana.

But I got it working with the following:

```sql
SELECT

  time_bucket AS "Time",

${aggregation_value}(value) AS "A Phase Voltage"

FROM (

SELECT

CASE

WHEN '${group_interval:raw}' = 'raw' THEN time

ELSE date_bin(INTERVAL '${group_interval:raw}', time)

END AS time_bucket,

value

FROM spine

WHERE

    point = 'a_voltage'

AND time >= $__timeFrom()

AND time <= $__timeTo()

)

GROUP BY time_bucket

```
