Query to Show on Grafana as Stacked Bars

Viewed 9

I am trying to write a MYSQL Query in Grafana to show Stacked Bar Graphs but I am not able to get the data in desired format.

This is the query that I am performing.

WITH required_data  AS (
  SELECT
    t1.id,
    t1.name,
    t1.region,
    t1.hours,
    t1.date
  FROM tbl1 AS t1
  WHERE t1.date >= '2022-09-01' 
  AND t1.date <= '2022-09-30'
)
SELECT
  required_data.name,
  required_data.region,
  SUM(required_data.hours) AS `Hours`
FROM required_data
JOIN tbl2 AS t2
ON t2.id = t1.id
GROUP BY
    required_usage_data.name,
    required_usage_data.region;

The result I get from this query is this which is not the desired way to create stacked bars in grafana.

| name | region         | hours |
|------|----------------|-------|
| ABC  | us-east-1      | 43    |
| EFG  | us-east-1      | 56    |
| PQR  | us-east-1      | 12    |
| ABC  | eu-central-1   | 67    |
| EFG  | eu-central-1   | 78    |
| ABC  | ap-south-1     | 90    |
| EFG  | ap-south-1     | 332   |
| PQR  | ap-northeast-1 | 5     |

I want to plot distinct names on the X-axis and stack the bar for that name vertically with different sections of region and summation hours of all the names in different region as Y-axis.

enter image description here

How should a form my query to get the result?

0 Answers
Related