# How do I create a DB query with variable parameters?

**URL:** <https://discuss.elastic.co/t/how-do-i-create-a-db-query-with-variable-parameters/149818>\
**Category:** Logstash\
**Created:** [September 25, 2018, 12:19pm UTC](https://discuss.elastic.co/t/how-do-i-create-a-db-query-with-variable-parameters/149818 "2018-09-25T12:19:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sversienti](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sversienti](https://discuss.elastic.co/u/sversienti)\
**Post date:** [September 25, 2018, 12:19pm UTC](https://discuss.elastic.co/t/how-do-i-create-a-db-query-with-variable-parameters/149818/1 "2018-09-25T12:19:50Z")

</div>

Hi,  
I'd like to set up a parameterized query. I configured my **logstash.conf** to query my Oracle database. My table has a `REF_DATE` field valued with a date `CHAR(10 BYTE)`.  
I need to make the following query:

```
`SELECT COUNT (*) FROM MY_TABLE WHERE REF_DATE = <TODAY_DATE>`

```

This query will be executed one or more time every day and `<TODAY_DATE>` will be change every day.  
I tried to use the `:sql_last_value` parameter but I did not reach my goal.

Is it possibile to configure `<TODAY_DATE>` into **logstash.conf** or sending this parameter from kibana tool?

Could you help me please?  
Thank you

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [September 26, 2018, 12:41am UTC](https://discuss.elastic.co/t/how-do-i-create-a-db-query-with-variable-parameters/149818/2 "2018-09-26T00:41:14Z")

</div>

Could this be done with the Oracle `CURRENT_DATE`?

```auto
SELECT COUNT (*) FROM MY_TABLE WHERE REF_DATE = TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD')

```

---

<div class="post-metadata">

**Author:** ![sversienti](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sversienti](https://discuss.elastic.co/u/sversienti)\
**Post date:** [September 26, 2018, 7:21am UTC](https://discuss.elastic.co/t/how-do-i-create-a-db-query-with-variable-parameters/149818/3 "2018-09-26T07:21:42Z")

</div>

Thanks! It is useful but in my question I forgot to write that I need to query even for past dates, like for example:

`SELECT COUNT (*) FROM MY_TABLE WHERE REF_DATE = TO_CHAR(PAST_DATE, 'YYYY-MM-DD')`

How can I pass a variable past date as input to my query?  
Thank you.

---

<div class="post-metadata">

**Author:** ![yaauie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yaauie/32/23363_2.png) [@yaauie](https://discuss.elastic.co/u/yaauie)\
**Post date:** [September 26, 2018, 7:42am UTC](https://discuss.elastic.co/t/how-do-i-create-a-db-query-with-variable-parameters/149818/4 "2018-09-26T07:42:42Z")

</div>

That could be done with a `GROUP BY` clause, which would emit one event per `REF_DATE` value:

```auto
SELECT REF_DATE, COUNT(*) FROM MY_TABLE GROUP BY REF_DATE

```

---

<div class="post-metadata">

**Author:** ![sversienti](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@sversienti](https://discuss.elastic.co/u/sversienti)\
**Post date:** [September 26, 2018, 12:27pm UTC](https://discuss.elastic.co/t/how-do-i-create-a-db-query-with-variable-parameters/149818/5 "2018-09-26T12:27:27Z")

</div>

Thanks yaauie! I understood how to do it.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 24, 2018, 12:38pm UTC](https://discuss.elastic.co/t/how-do-i-create-a-db-query-with-variable-parameters/149818/6 "2018-10-24T12:38:40Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
