# Group field by name and date

**URL:** <https://discuss.elastic.co/t/group-field-by-name-and-date/185460>\
**Category:** Elasticsearch\
**Created:** [June 12, 2019, 3:35pm UTC](https://discuss.elastic.co/t/group-field-by-name-and-date/185460 "2019-06-12T15:35:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_O](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_o/32/47985_2.png) [@Jan\_O](https://discuss.elastic.co/u/Jan_O)\
**Post date:** [June 12, 2019, 3:35pm UTC](https://discuss.elastic.co/t/group-field-by-name-and-date/185460/1 "2019-06-12T15:35:37Z")

</div>

Hi,  
I have been looking for a way to group information by name and date.  
We have a setup with thousands of tests running on different products, what I want to do is fetch all failing test cases but only count them once per date. So if a test has failed on 5 different products it should only be registered as one fail that specific day.

I guess this would be the corresponding sql queery if it was a mysql database:

```
SELECT test_name, count(DISTINCT DATE(timestamp))
  FROM mysql_test
  GROUP by test_name;

```

Thanks and all the best

---

<div class="post-metadata">

**Author:** ![Andrei\_Stefan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andrei_stefan/32/47533_2.png) [@Andrei\_Stefan](https://discuss.elastic.co/u/Andrei_Stefan)\
**Post date:** [June 13, 2019, 8:50am UTC](https://discuss.elastic.co/t/group-field-by-name-and-date/185460/2 "2019-06-13T08:50:00Z")

</div>

Hi @Jan_O,

Slightly unrelated, but you can use the [`translate` API of Elasticsearch SQL](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/sql-translate.html) to help you create a query DSL from that sql query. To give you an example, something like this should help you:

```auto
{
    "size": 0,
    "_source": false,
    "stored_fields": "_none_",
    "aggregations": {
        "groupby": {
            "composite": {
                "size": 1000,
                "sources": [
                    {
                        "13": {
                            "terms": {
                                "field": "test_name",
                                "missing_bucket": true,
                                "order": "asc"
                            }
                        }
                    }
                ]
            },
            "aggregations": {
                "22": {
                    "cardinality": {
                        "field": "timestamp"
                    }
                }
            }
        }
    }
}

```

If timestamp is in millis, though, this won't help you.  
If it is in millis, you need to add scripting to your `cardinality` aggregation above and convert your timestamps in dates (no time component). How to define scripts is [here](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-cardinality-aggregation.html#_script_3) in the documentation.

Hope this helps.

---

<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:** [July 11, 2019, 8:50am UTC](https://discuss.elastic.co/t/group-field-by-name-and-date/185460/3 "2019-07-11T08:50:02Z")

</div>

Closed on July 11, 2019, 8:50am UTC

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