title: InfluxDB aggregate on TOP function and display in Grafana
body: <p>I'm new to InfluxDB and Grafana. I'm using version 1.7.4 and playing through the Grafana Twitter example in the Spring Cloud Data Flow workshop. There is basically a <code>hashtags</code> measurement like this:</p>
  
  <pre><code>select * from hashtags limit 10
  name: hashtags
  time                       htag             value
  ----                       ----             -----
  2019-03-07T07:46:59.096Z   現実見ろ           0    
  2019-03-07T07:46:59.097Z   キズのしょち松さん   0    
  2019-03-07T07:46:59.097Z   競馬              0    
  2019-03-07T07:46:59.098Z   PerthTanapon     0    
  2019-03-07T07:46:59.098Z   終活              0    
  2019-03-07T07:46:59.106Z   capolavoro       0    
  2019-03-07T07:46:59.108Z   Cenizas          0    
  2019-03-07T07:46:59.108Z   design           0    
  2019-03-07T07:46:59.108Z   hyungwon         0    
  2019-03-07T07:46:59.108Z   ざわちん           0    
  </code></pre>
  
  <p>In the <a href="https://youtu.be/hWLhrXIepLs?t=354" rel="nofollow noreferrer">example screencast</a> the author creates a pie chart that is supposed to display the top 10 hashtags on Twitter. The video shows that he creates a query like this:</p>
  
  <p><code>SELECT sum("value") FROM "hashtags" GROUP BY "htag" SLIMIT 10</code></p>
  
  <p>The problem here is that the chart only represents 10 hashtags in alphabetical order. What I want is the Top 10 hashtags. Now InfluxDB doesn't have a order by clause on tags or fields so I tried to come up with my own query that tries to solve my problem using <code>TOP()</code> and a subquery:</p>
  
  <p><code>select top("sum", "htag",10) from (SELECT sum("value") FROM "hashtags" GROUP BY "htag")</code> which yields the correct result:</p>
  
  <pre><code>name: hashtags
  time                 top  htag
  ----                 ---  ----
  1970-01-01T00:00:00Z 3172 TwoOfUs
  1970-01-01T00:00:00Z 2301 BestCoverSong
  1970-01-01T00:00:00Z 1480 เชงเม้ง
  1970-01-01T00:00:00Z 1337 투모로우바이투게더
  1970-01-01T00:00:00Z 1117 GOT7
  1970-01-01T00:00:00Z 1106 MeMyselfAndI
  1970-01-01T00:00:00Z 848  백현
  1970-01-01T00:00:00Z 848  ยุบพรรค
  1970-01-01T00:00:00Z 842  Jus2
  1970-01-01T00:00:00Z 781  TOMORROW_X_TOGETHER
  </code></pre>
  
  <p>The problem with grafana is that it doesn't properly display my results in the pie chart</p>
  
  <p><a href="https://i.stack.imgur.com/CtG1N.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CtG1N.png" alt="enter image description here"></a></p>
  
  <p>What am I missing? I saw that the first query has a grouping so the query result looks like this:</p>
  
  <pre><code>name: hashtags
  tags: htag=01JokowiAs1k
  time                 sum
  ----                 ---
  1970-01-01T00:00:00Z 0
  
  name: hashtags
  tags: htag=01JokowiLagi
  time                 sum
  ----                 ---
  1970-01-01T00:00:00Z 9
  
  name: hashtags
  tags: htag=01OptimisIndonesiaMaju
  time                 sum
  ----                 ---
  1970-01-01T00:00:00Z 7
  </code></pre>
  
  <p>How can I achieve that grouping on my query?</p>