# Elasticsearch SQL ORDER BY Month

**URL:** https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716
**Category:** Elasticsearch
**Tags:** elastic-stack-sql
**Created:** [November 25, 2020, 10:00pm UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716 "2020-11-25T22:00:02Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![willemdh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/willemdh/32/16922_2.png) [@willemdh](https://discuss.elastic.co/u/willemdh)
#### Post date: [November 25, 2020, 10:00pm UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716/1 "2020-11-25T22:00:02Z")

</div>

Hello,

I'm almost there getting my vertical bar to work in Canvas based on a SQL query, but I'm having a hard time ordering the monthly buckets chronologically. My SQL query:

```
SELECT COUNT(DISTINCT user.name) AS UniqueUsers,
MONTH_NAME("@timestamp") AS Month
FROM "nagios" WHERE "@timestamp" > now() - interval 1 years AND event.dataset LIKE 'nagios.audit' AND MATCH(message,'Logged in')
GROUP BY Month
ORDER BY Month

```

Result:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/a/fa574b830246bc1e15e8ea923b43a29949d9e8eb.png)

So how can I get the months ordered by @timestamp?

Grtz

Willem

---

<div class="post-metadata">

### Author: ![bogdan.pintea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bogdan.pintea/32/45740_2.png) [@bogdan.pintea](https://discuss.elastic.co/u/bogdan.pintea)
#### Post date: [November 27, 2020, 9:03am UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716/2 "2020-11-27T09:03:54Z")

</div>

Instead/besides `MONTH_NAME` you could GROUP BY [MONTH](https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-functions-datetime.html#sql-functions-datetime-month).

---

<div class="post-metadata">

### Author: ![willemdh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/willemdh/32/16922_2.png) [@willemdh](https://discuss.elastic.co/u/willemdh)
#### Post date: [November 27, 2020, 9:48am UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716/3 "2020-11-27T09:48:02Z")

</div>

Hello @bogdan.pintea,

Thanks a lot for your answer. In the meantime we are +- using something similar to what you suggest:

```
SELECT COUNT(DISTINCT user.name) AS UniqueUsers,
MONTH_NAME("@timestamp") AS Month,
MONTH("@timestamp") AS MonthNumber
FROM "nagios" WHERE "@timestamp" > now() - interval 1 years AND event.dataset LIKE 'nagios.audit' AND MATCH(message,'Logged in')
GROUP BY Month, MonthNumber
ORDER BY MonthNumber 

```

The result is:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/f/6/f6508e6446b5f922ca84f90ee2a9f1670b41bca4.png)

But as you can see the graph start with month 1 (january). As it's november currently, the first bucket in the graph should be december last year. I think we should somehow evolve to a group by / sort based on a combination of year and month. But not sure how to accomplish that.

Grtz

Willem

---

<div class="post-metadata">

### Author: ![willemdh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/willemdh/32/16922_2.png) [@willemdh](https://discuss.elastic.co/u/willemdh)
#### Post date: [December 16, 2020, 9:27am UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716/4 "2020-12-16T09:27:50Z")

</div>

(Trying to prevent auto close) So ordering buckets chronologically with essql is possible or not?

---

<div class="post-metadata">

### Author: ![bogdan.pintea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bogdan.pintea/32/45740_2.png) [@bogdan.pintea](https://discuss.elastic.co/u/bogdan.pintea)
#### Post date: [December 16, 2020, 10:36am UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716/5 "2020-12-16T10:36:17Z")

</div>

@willemdh, maybe something like this would work for you?

```auto
SELECT COUNT(DISTINCT user.name), DATETIME_FORMAT("@timestamp", 'MMM')
FROM ...
WHERE ...
GROUP BY DATETIME_FORMAT("@timestamp", 'yyyy-LL'), 2
ORDER BY ...

```

---

<div class="post-metadata">

### Author: ![willemdh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/willemdh/32/16922_2.png) [@willemdh](https://discuss.elastic.co/u/willemdh)
#### Post date: [December 16, 2020, 6:18pm UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716/6 "2020-12-16T18:18:48Z")

</div>

Thanks @bogdan.pintea for your answer.

The data preview seems perfect:

![image](https://us1.discourse-cdn.com/elastic/original/3X/f/3/f381b2a9934d5f564ba6f37600e7accf0fbeaa26.png)

But I'm having some troubles configuring the graph when I don't use as 'AS', seems something wrong with using "@timestamp".

![image](https://us1.discourse-cdn.com/elastic/original/3X/0/1/01f68b58816c75693ea0da859c339a9e49cb4267.png)

So tried with 'AS':

```
SELECT COUNT(DISTINCT user.name) AS UniqueUsers, DATETIME_FORMAT("@timestamp", 'MMM') AS Month
FROM "nagios" 
WHERE "@timestamp" > now() - interval 1 years AND event.dataset LIKE 'nagios.audit' AND MATCH(message,'Logged in')
GROUP BY DATETIME_FORMAT("@timestamp", 'yyyy-LL'), 2

```

Which produces:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/d/4d7523fce39f0a16e60b5a023a3882b95ebc437b.png)

But this is also ordered alphabetically..

So it seems to result in the same issue using "GROUP BY Month, MonthNumber".

Willem

---

<div class="post-metadata">

### Author: ![bogdan.pintea](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bogdan.pintea/32/45740_2.png) [@bogdan.pintea](https://discuss.elastic.co/u/bogdan.pintea)
#### Post date: [December 17, 2020, 12:13pm UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716/7 "2020-12-17T12:13:02Z")

</div>

I see.  
This seems to be a Canvas-specific behaviour. My Canvas kung-fu is not that strong unfortunately, but while trying to replicate your experience I've noticed that a `null` X-axis value will trigger the plot to reorder the X values. I don't see a `null` value in your list, but if you think there is - and just not rendered - you might want to try filtering it out: add a `AND Month IS NOT NULL` to your WHERE clause.

You could try to play with the expression in the `</> Expression editor` and potentially add a `| head count=<some number>` (or `| tail ..`) after `| essql ` function to try find out if there is a value triggering the reorder.  
Even better, maybe ask in the Kibana forum.

You could also try to generate a name for the months which is immutable to reordering, but I feel there should be an easier way to get what you want.

---

<div class="post-metadata">

### Author: ![willemdh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/willemdh/32/16922_2.png) [@willemdh](https://discuss.elastic.co/u/willemdh)
#### Post date: [December 18, 2020, 7:57am UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716/8 "2020-12-18T07:57:10Z")

</div>

Created [Canvas Group By Month with Chronological order](https://discuss.elastic.co/t/canvas-group-by-month-with-chronological-order/259066)

Sorry for the many questions and ty very much for the insights @bogdan.pintea

---

<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: [January 15, 2021, 7:57am UTC](https://discuss.elastic.co/t/elasticsearch-sql-order-by-month/256716/9 "2021-01-15T07:57:13Z")

</div>

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