# Using SQL queries, creating one column per each distinct value of columns used in Group By clause

**URL:** <https://community.grafana.com/t/using-sql-queries-creating-one-column-per-each-distinct-value-of-columns-used-in-group-by-clause/117403>\
**Category:** Bar Chart Panel\
**Tags:** sql\
**Created:** [March 19, 2024, 11:19pm UTC](https://community.grafana.com/t/using-sql-queries-creating-one-column-per-each-distinct-value-of-columns-used-in-group-by-clause/117403 "2024-03-19T23:19:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![amirkamali](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@amirkamali](https://community.grafana.com/u/amirkamali)\
**Post date:** [March 19, 2024, 11:19pm UTC](https://community.grafana.com/t/using-sql-queries-creating-one-column-per-each-distinct-value-of-columns-used-in-group-by-clause/117403/1 "2024-03-19T23:19:57Z")

</div>

- What Grafana version and what operating system are you using?  
Grafana v9.5.3 (916d9793aa)

- **What** are you trying to achieve?  
I am using Databricks clusters as data source. My query looks like this:  
SELECT time\_col, status, sum(count) AS count  
FROM ,y\_table  
WHERE $\_\_timeFilter(time\_col)  
GROUP BY time\_col, status  
ORDER BY time\_col, status

I want to have a stacked bar chart. Each stack has the sum(count) of different status stacked on top of each other for a given value of time\_col.  
I thought what I need is the count column pivoted based on values in status column.  
Example: if status column has two distinct value: status\_1 and status\_2; I want to have such a result:

| time\_col | count\_status\_1 | count\_status\_2 |
| --- | --- | --- |

```
  .... | ...... | .....

```

- **How** are you trying to achieve it?  
I was thinking Grafana handles it. I had seen examples before. But I am not sure how they are done. I tried `color by col` option from `Bar Chart` Setting side panel.

- What happened?  
Group by, by itself does not do the trick. Using color by col also didn’t help. I see different status values, but they are all treated as a single column and with the same color without being able to stack them.

- Did you follow any online instructions? If so, what is the URL?  
No

---

<div class="post-metadata">

**Author:** ![amirkamali](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@amirkamali](https://community.grafana.com/u/amirkamali)\
**Post date:** [March 22, 2024, 4:59pm UTC](https://community.grafana.com/t/using-sql-queries-creating-one-column-per-each-distinct-value-of-columns-used-in-group-by-clause/117403/2 "2024-03-22T16:59:37Z")

</div>

Solved. I understood that to achieve what I was looking for, I just need to set a transform as Follows:

Transform → Prepare time series → Multi-frame time series

The SQL itself just needs to group by the column you want to split the time series by. Also, that column should exist in the group by clause.
