<p>I have a custom field called releaseDate in one of my entry types. I would like to use Element API to get all the distinct years assigned to those entries. I dump all the years for all entries just fine like this:</p>

<pre><code>'endpoints' =&gt; [
    'api/v1/release_years' =&gt; function () {
    return [
        'class' =&gt; EntryResource::class,
        'elementType' =&gt; Entry::class,
        'criteria' =&gt; [
            'section' =&gt; 'products',
            'orderBy' =&gt; 'field_releaseDate',
            'where' =&gt; 'field_releaseDate IS NOT NULL',
//          'groupBy' =&gt; '????????'
        ],
        'transformer' =&gt; function (Entry $entry) {
            return [$entry-&gt;releaseDate-&gt;format('Y')];
        },
        'pretty' =&gt; true
    ];
}
</code></pre>

<p>But the minute I add a group clause to the criteria section, it fails. I've tried the following:</p>

<pre><code>'groupBy' =&gt; 'YEAR(field_relaseDate)'

'groupBy' =&gt; 'YEAR(relaseDate)'

'groupBy' =&gt; 'field_releaseDate.year'

'groupBy' =&gt; 'releaseDate.year'

'group' =&gt; 'YEAR(field_relaseDate)'

'group' =&gt; 'YEAR(relaseDate)'

'group' =&gt; 'field_releaseDate.year'

'group' =&gt; 'releaseDate.year'
</code></pre>

<p>Any ideas if this is even possible? Also does anyone know if you can return a single array from Element API instead of a multi-level array? This output seems a bit inefficient:</p>

<pre><code>'data' = [
    [2010],
    [2011],
    [2012],
    [2013],
    [2014],
    [2015],
    [2016],
    [2017],
    [2018],
    [2019],
]
</code></pre>

<p>I would much prefer:</p>

<pre><code>'data' = [
    2011,
    2012,
    2013,
    2014,
    2015,
    2016,
    2017,
    2018,
    2019
]
</code></pre>

CC BY-SA 4.0
2019-05-31T03:20:04.897Z
Did you manage to solve this?
j00lz
CC BY-SA 4.0
2019-06-07T14:30:21.900Z
Nope. I ended up making another entry type for release years and using a hook to add data dynamically to it. It ended up working ok since we eventually had to add other meta data to the release years.
Tony DeStefano
<craft3><plugin-elementapi><elementquery>
Group by Date Field in Element API Query
# Group by Date Field in Element API Query

(Asked by: username_0 on 2019-01-29)

I have a custom field called releaseDate in one of my entry types. I would like to use Element API to get all the distinct years assigned to those entries. I dump all the years for all entries just fine like this:
'endpoints' => [
    'api/v1/release_years' => function () {
    return [
        'class' => EntryResource::class,
        'elementType' => Entry::class,
        'criteria' => [
            'section' => 'products',
            'orderBy' => 'field_releaseDate',
            'where' => 'field_releaseDate IS NOT NULL',
//          'groupBy' => '????????'
        ],
        'transformer' => function (Entry $entry) {
            return [$entry->releaseDate->format('Y')];
        },
        'pretty' => true
    ];
}

But the minute I add a group clause to the criteria section, it fails. I've tried the following:
'groupBy' => 'YEAR(field_relaseDate)'

'groupBy' => 'YEAR(relaseDate)'

'groupBy' => 'field_releaseDate.year'

'groupBy' => 'releaseDate.year'

'group' => 'YEAR(field_relaseDate)'

'group' => 'YEAR(relaseDate)'

'group' => 'field_releaseDate.year'

'group' => 'releaseDate.year'

Any ideas if this is even possible? Also does anyone know if you can return a single array from Element API instead of a multi-level array? This output seems a bit inefficient:
'data' = [
    [2010],
    [2011],
    [2012],
    [2013],
    [2014],
    [2015],
    [2016],
    [2017],
    [2018],
    [2019],
]

I would much prefer:
'data' = [
    2011,
    2012,
    2013,
    2014,
    2015,
    2016,
    2017,
    2018,
    2019
]

**Tags:** craft3, plugin-elementapi, elementquery
> **Comments:**
> **username_1**: Did you manage to solve this?  
> **username_0**: Nope. I ended up making another entry type for release years and using a hook to add data dynamically to it. It ended up working ok since we eventually had to add other meta data to the release years.  

---

## 0 answers

