# Time range query

**URL:** <https://community.grafana.com/t/time-range-query/46831>\
**Category:** Configuration\
**Created:** [April 29, 2021, 8:21pm UTC](https://community.grafana.com/t/time-range-query/46831 "2021-04-29T20:21:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![szbiro](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@szbiro](https://community.grafana.com/u/szbiro)\
**Post date:** [April 29, 2021, 8:21pm UTC](https://community.grafana.com/t/time-range-query/46831/1 "2021-04-29T20:21:44Z")

</div>

Hello,  
I installed grafana, but my sql skills are way below the level of the presentation in the documentation regarding my “issue”. Please excuse me if there happens to be a dedicated feature/chapter which exactly would do this. I simply can’t understand/find it there.  
I connected my sqlite db file through the sqlite datasource and doing a simple single query of my table like;  
SELECT DateTimeStamp, Temperature FROM sensordata  
It’s a timeseries table and the DateTimeStamp column is set as a time formatted column.

![image](https://us1.discourse-cdn.com/grafana/original/3X/4/5/4599502d690ae22e505c13fa20eba861f8a2474f.png)

The table is starting to be big and the query time is taking longer and longer, which is normal because i am doing a complete query of the table.  
 ![image](https://us1.discourse-cdn.com/grafana/original/3X/b/a/bacf2ab03d0cb7e22672bc60aeb3117acca0892d.png)

My question is if there is a way to connect or corelate the relative or absolute timerange  
dropdown list elements to query only the selected time range and don’t do a full query then jump to that time range.

![image](https://us1.discourse-cdn.com/grafana/original/3X/4/d/4df90eab02d8c480fd27bab404b8dfc5174e1de4.png)

I searched for keywords like dynamic, time, range, query on google and here but nothing that i would understand came up.  
Thank you kindly and have a nice evening if applies.

---

<div class="post-metadata">

**Author:** ![anon70503803](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@anon70503803](https://community.grafana.com/u/anon70503803)\
**Post date:** [May 3, 2021, 8:54pm UTC](https://community.grafana.com/t/time-range-query/46831/2 "2021-05-03T20:54:18Z")

</div>

Hi @szbiro ,

The kind of functionality you’re talking about – creating dynamic queries based on a time range – is totally possible in other, more robust SQL databases like MySQL and Postgres. The typical method uses macros, I believe. [Here are the macros for the MySQL datasource plugin](https://grafana.com/docs/grafana/latest/datasources/mysql/#macros).

For instance, here is a query I have using that plugin:

```sql
SELECT
  UNIX_TIMESTAMP(TIMEST) as time_sec,
  VARIABLE_VALUE+0 as value,
  VARIABLE_NAME as metric
FROM my2.status
WHERE $__timeFilter(TIMEST)
AND variable_name in ('THREADS_CONNECTED', 'THREADS_RUNNING')
ORDER BY TIMEST ASC

```

note the line `WHERE $__timeFilter(TIMEST)`.

Unfortunately, according to the [github repo for the SQLite datasource](https://github.com/fr-ser/grafana-sqlite-datasource#macros), these macros are not supported.

The author does list two supported macros:

`$__unixEpochGroupSeconds(unixEpochColumnName, intervalInSeconds)`  
and  
`$__unixEpochGroupSeconds(unixEpochColumnName, intervalInSeconds, NULL) `

So, this might be totally possible with SQLite, but you will find more more functionality and supporting documentation if you switch to a DB like MySQL or Postgres. [Here’s a nice article detailing the differences between the three major relational databases](https://www.digitalocean.com/community/tutorials/sqlite-vs-mysql-vs-postgresql-a-comparison-of-relational-database-management-systems).

I hope this helps a bit!

---

<div class="post-metadata">

**Author:** ![szbiro](https://avatars.discourse-cdn.com/v4/letter/s/94ad74/32.png) [@szbiro](https://community.grafana.com/u/szbiro)\
**Post date:** [May 6, 2021, 5:57pm UTC](https://community.grafana.com/t/time-range-query/46831/3 "2021-05-06T17:57:12Z")

</div>

my god, i envy people like you (decoding “kitchen language” and solving the problem after reading the documentation, not to mention you dont even use sqlite i assume).  
thank you btw i already changed from sqlite to influxdb for this exact reason. Since influx natively does dynamic query after i link my db everything works like a charm.

---

<div class="post-metadata">

**Author:** ![anon70503803](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@anon70503803](https://community.grafana.com/u/anon70503803)\
**Post date:** [May 6, 2021, 7:43pm UTC](https://community.grafana.com/t/time-range-query/46831/4 "2021-05-06T19:43:50Z")

</div>

Wise decision, switching to influx. I think you’ll find much more support and functionality using that db!

There are some influx wizards (not me, unfortunately!) lurking in this forum, so as you continue to explore that product, don’t hesitate to keep posting questions 👍
