# SQL Lite aggregare on non grouped Column

**URL:** <https://discuss.elastic.co/t/sql-lite-aggregare-on-non-grouped-column/343539>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-sql\
**Created:** [September 21, 2023, 12:24pm UTC](https://discuss.elastic.co/t/sql-lite-aggregare-on-non-grouped-column/343539 "2023-09-21T12:24:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harinder\_Singh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harinder_singh/32/106262_2.png) [@Harinder\_Singh](https://discuss.elastic.co/u/Harinder_Singh)\
**Post date:** [September 21, 2023, 12:24pm UTC](https://discuss.elastic.co/t/sql-lite-aggregare-on-non-grouped-column/343539/1 "2023-09-21T12:24:06Z")

</div>

Hi @leandrojmp ,

I have a requirement where I need to apply order on a filed in Elasticsearch which is formed dynamically on the fly and it is not part of original index.

My Sample document looks like

````auto
{
  "deviceType": "desktop",
  "personalizedSignal": "sdf",
  "componentPosition": "top",
  "count": 10,
  "environment": "sdfsdf",
  "pageType": "hp",
  "eventName": "click",
  "analyticsID": "sdfsdf",
  "timestamp": 1691589600000
}

{
  "deviceType": "desktop",
  "personalizedSignal": "sdf",
  "componentPosition": "top",
  "count": 10,
  "environment": "sdfsdf",
  "pageType": "hp",
  "eventName": "impression",
  "analyticsID": "sdfsdf",
  "timestamp": 1691589600000
}```

I am running sqllite statement from Superset where I need to display and order by a column which is determined on the fly

Query

````

GET \_sql?format=txt  
{  
"query": """  
SELECT analyticsID,  
impression,  
click,  
(1.0 \* click/impression) as CTR  
from  
(select analyticsID,  
SUM(case  
when eventName='impression' then (count)  
end) impression,  
SUM(case  
when eventName='click' then (count)  
end) click  
from "rollup-dco-dummy-index"  
group by analyticsID) order by CTR DESC  
"""  
}

```auto

I am getting an error like below

{
  "error": {
    "root_cause": [
      {
        "type": "verification_exception",
        "reason": """Found 1 problem
line 15:41: Cannot order by non-grouped column [CTR], expected [analyticsID] or an aggregate function"""
      }
    ],
    "type": "verification_exception",
    "reason": """Found 1 problem
line 15:41: Cannot order by non-grouped column [CTR], expected [analyticsID] or an aggregate function"""
  },
  "status": 400
}
```

---

<div class="post-metadata">

**Author:** ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)\
**Post date:** [September 21, 2023, 12:32pm UTC](https://discuss.elastic.co/t/sql-lite-aggregare-on-non-grouped-column/343539/2 "2023-09-21T12:32:23Z")

</div>

Hello,

Sorry, I cannot help as I never used the SQL interface in Elasticsearch, I have no experience with 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 19, 2023, 12:32pm UTC](https://discuss.elastic.co/t/sql-lite-aggregare-on-non-grouped-column/343539/3 "2023-10-19T12:32:53Z")

</div>

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