# Using DATEADD() in SELECT column list causes later columns to be ignored

**URL:** <https://community.questdb.com/t/using-dateadd-in-select-column-list-causes-later-columns-to-be-ignored/244>\
**Category:** Community\
**Created:** [November 25, 2024, 4:45am UTC](https://community.questdb.com/t/using-dateadd-in-select-column-list-causes-later-columns-to-be-ignored/244 "2024-11-25T04:45:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjs3273](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.questdb.com/rjs3273/32/137_2.png) [@rjs3273](https://community.questdb.com/u/rjs3273)\
**Post date:** [November 25, 2024, 4:45am UTC](https://community.questdb.com/t/using-dateadd-in-select-column-list-causes-later-columns-to-be-ignored/244/1 "2024-11-25T04:45:46Z")

</div>

I have a query in which I want to take all columns from a table, but modify the time field. As an example, here I get all fields in my table but subtract one month from the timestamp.

```auto
select "system", sensor, sensordesc, avg(energy) as energy, avg(voltage) as voltage,
    dateadd('M', -1, "timestamp") as "timestamp", vbus, sensorname 
    from "emporia"
    where timestamp in '2024-11-24' sample by 10m

```

It works perfectly except that the final column of my SELECT (“sensordesc”) does not get returned. I get all columns up to and including “vbus” correctly. Odd and inconsistent things happen for any columns I place in the SELECT list after the DATEADD() function.

If I specify the column name twice, then it all works and I get all the columns as requested, with the column I asked for twice only appearing once. I.e., the following command does what I was expecting from the original command, returning every column once.

```auto
select "system", sensor, sensordesc, avg(energy) as energy, avg(voltage) as voltage,
    dateadd('M', -1, "timestamp") as "timestamp", vbus, sensorname, sensorname 
    from "emporia"
    where timestamp in '2024-11-24' sample by 10m

```

Is this expected behaviour? I could not find anything in the docs about not using dateadd() in SELECT lists.

QuestDB 8.1.4 running in a docker using image “questdb/questdb:latest”

 ![Screenshot 2024-11-24 at 9.29.34 PM](https://canada1.discourse-cdn.com/flex004/uploads/questdb/original/1X/682c2461bebb33bfbdd5d1e95e48689f6511cb8f.jpeg)

---

<div class="post-metadata">

**Author:** ![rjs3273](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.questdb.com/rjs3273/32/137_2.png) [@rjs3273](https://community.questdb.com/u/rjs3273)\
**Post date:** [November 25, 2024, 4:46am UTC](https://community.questdb.com/t/using-dateadd-in-select-column-list-causes-later-columns-to-be-ignored/244/2 "2024-11-25T04:46:50Z")

</div>

And a screen shot of getting the correct result when I ask for “sensorname” twice.

 ![Screenshot 2024-11-24 at 9.29.58 PM](https://canada1.discourse-cdn.com/flex004/uploads/questdb/original/1X/ea3629408decda14255effb4af03df656b1acf84.jpeg)

---

<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:** [November 25, 2024, 11:05am UTC](https://community.questdb.com/t/using-dateadd-in-select-column-list-causes-later-columns-to-be-ignored/244/3 "2024-11-25T11:05:44Z")

</div>

Hi @rjs3273 ,

This is a bug related to an optimisation that converts `SAMPLE BY` into a `GROUP BY` clause that runs in parallel. I thought we had an open issue for this, but I’m unable to locate it.

It is certainly **not** expected behaviour. If you move your `dateadd` to be the final column of your query, it should work fine.

We will aim to fix this in the next release!

---

<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:** [November 25, 2024, 1:14pm UTC](https://community.questdb.com/t/using-dateadd-in-select-column-list-causes-later-columns-to-be-ignored/244/4 "2024-11-25T13:14:47Z")

</div>

Follow up - did a quick test of this PR and this should resolve the issue.

> <https://github.com/questdb/questdb/pull/5113>
>
> Fixes https://github.com/questdb/questdb/issues/5105

Tested with:

```sql
select dateadd('d', 1, timestamp) "timestamp" , symbol from trades
sample by 10m

```

---

<div class="post-metadata">

**Author:** ![rjs3273](https://yyz2.discourse-cdn.com/flex004/user_avatar/community.questdb.com/rjs3273/32/137_2.png) [@rjs3273](https://community.questdb.com/u/rjs3273)\
**Post date:** [November 25, 2024, 4:06pm UTC](https://community.questdb.com/t/using-dateadd-in-select-column-list-causes-later-columns-to-be-ignored/244/5 "2024-11-25T16:06:50Z")

</div>

Thanks. Good to know it is a known issue and there looks to be a solution. For the time being I have a solid workaround anyway.

For the record, putting the dateadd() last does not work as a workaround for me. When I do that the date field gets lost altogether. I can workaround just by naming the field twice.

---

<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:** [November 25, 2024, 4:19pm UTC](https://community.questdb.com/t/using-dateadd-in-select-column-list-causes-later-columns-to-be-ignored/244/6 "2024-11-25T16:19:45Z")

</div>

That’s strange! We’ll try to get this fixed ASAP, and at least you have it working for now.
