1

So I'm just building a small project that involves the use of MySQL and Grafana.

Here's my MySQL query and it's corresponding correct output:

select state, sum(total_cases) from PolDATA group by state order by sum(total_cases) desc limit 5;

Correct output: Output

I have enetered the same query in Grafana as well. Also, the format is Table and not Time Series. The output in Grafana is this: Grafana output when I display as a gauge. Here I do not want the text to be sum(total_cases). I want it to display the name of the state. I tried altering it in Visualization->Field->Title but that changes all the labels to the same value. I even tried adding to the query select sum(total_cases) as "Sometext" but it changes all the labels to "Sometext" instead.

Help is appreciated. Thank you (:

forpas
  • 160,666
  • 10
  • 38
  • 76

1 Answers1

1
  • First you need to configure your Grafana as the below image. enter image description here

  • You need to write pivot table query as below.

select
sum(case when state = 'MAHARASHTRA'  then total_cases else 0 end) as "MAHARASHTRA",
sum(case when state = 'GUJARAT'  then total_cases else 0 end) as "GUJARAT",
sum(case when state = 'MADHYA PRADESH'  then total_cases else 0 end) as "MADHYA PRADESH",
sum(case when state = 'RAJASTHAN'  then total_cases else 0 end) as "RAJASTHAN",
sum(case when state = 'DELHI'  then total_cases else 0 end) as "DELHI"
from PolDATA
group by state

E.g. enter image description here

Note: If your "state" column is dynamic, then you need to try dynamic pivoting.

E.g. https://stackoverflow.com/a/12599372/4249637