# Is it possible to rewrite SAMPLE BY query as GROUP BY in QuestDB?

**URL:** <https://community.questdb.com/t/is-it-possible-to-rewrite-sample-by-query-as-group-by-in-questdb/38>\
**Category:** Community\
**Tags:** sql, question\
**Created:** [June 26, 2024, 6:14pm UTC](https://community.questdb.com/t/is-it-possible-to-rewrite-sample-by-query-as-group-by-in-questdb/38 "2024-06-26T18:14:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex004/uploads/questdb/original/1X/b6b1b349e598d0550947e63d7923a9d7df0279eb.png) [@system](https://community.questdb.com/u/system)\
**Post date:** [June 26, 2024, 6:14pm UTC](https://community.questdb.com/t/is-it-possible-to-rewrite-sample-by-query-as-group-by-in-questdb/38/1 "2024-06-26T18:14:40Z")

</div>

I have the following SAMPLE BY query:

```auto
SELECT ts, min(temp), max(temp), avg(temp)
FROM temperature
SAMPLE BY 10s;

```

I’d like to rewrite it as a GROUP BY, so that QuestDB runs it multi-threaded. How can I do that? I’ve noticed `date_trunc(unit, timestamp)` in the docs, but the function doesn’t accept arguments like 10s.

---

<div class="post-metadata">

**Author:** ![nwoolmer](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.questdb.com/nwoolmer/32/24_2.png) [@nwoolmer](https://community.questdb.com/u/nwoolmer)\
**Post date:** [June 26, 2024, 6:15pm UTC](https://community.questdb.com/t/is-it-possible-to-rewrite-sample-by-query-as-group-by-in-questdb/38/2 "2024-06-26T18:15:00Z")

</div>

You can rewrite the query in the following way:

```auto
SELECT (ts - ts%10000000)::timestamp as ts, min(temp), max(temp), avg(temp)
FROM temperature
ORDER BY ts;

```

Here, we truncate the timestamp to 10 seconds (`ts - ts%10000000` - native QuestDB’s time resolution is in microseconds) and cast it from LONG back to TIMESTAMP column type. Notice that the `GROUP BY` clause is optional in QuestDB, so we omit it. The rest is as simple as ordering the result set time-wise with `ORDER BY ts`.
