Query on stacked bar graph

I am using grafana version 11.6 on linux machine

I want to show category wise sales on daily basis, I use postgreSql table (sales_summary) with 3 columns as data source.
sales_date
category
sales_amount.

I want to show this data in a stacked bar graph, where x-axis contain sales_date and y-axis contains sum of all sales_amount for each day. The bar should be stacked based on sales category.

I am trying to use the below query
SELECT DATE(sales_date), category, SUM(sales_amount) FROM sales_summary
GROUP BY DATE(sales_date), category
ORDER BY DATE(sales_date)

This does not show colored stack, but shows 2 bars for 2 categories both bars has same x-axis date.
I want to see them as one bar with 2 colors for 2 categories for single date.

Please let me know how can I achieve this

Hi,

Can you share a screen of how it looks now? From the description I would tell you need to change the Stacking option to normal and it should work, but it might not (depending on the data you have now).

If it doesn’t work can you share some sample of the data (preferably as a table - you can obtain that by toggling Table View above the chart).

1 Like

Yes. I have done stacking as “Normal”

Below is the sample data :
sale_date category sales_amount
08-02-2025 A 506
08-02-2025 B 106
08-02-2025 B 409
09-02-2025 A 138
09-02-2025 B 235
11-02-2025 A 169
12-02-2025 A 667
12-02-2025 B 736
12-02-2025 A 350
12-02-2025 A 368
13-02-2025 B 210
13-02-2025 B 101
13-02-2025 B 132
14-02-2025 B 107
14-02-2025 A 217
15-02-2025 A 101
15-02-2025 A 119

I am expecting one bar with x-axis value as “08-02-2025” with y-axis value as 1021 (506+106+409) as has 2 colors first color till 506 and second color for 505.

Now it is showing in single color.

Is the data you’ve provided raw from database or is it seen in Grafana query (as far as I know SQL, you shouldn’t have two different 08-02-2025 B values). If that’s the case, your data in Grafana should look like this:
08-02-2025 A 506
08-02-2025 B 515
09-02-2025 A 138
09-02-2025 B 235
11-02-2025 A 169
12-02-2025 A 1385
12-02-2025 B 736
13-02-2025 B 443
14-02-2025 B 107
14-02-2025 A 217
15-02-2025 A 220

Is that right (when toggling the Table View in Grafana)?
If so, what you need is transform the data to a matrix (the first transformation on the screen below). I also included the convert field type transformation but in your case it might not be needed.

1 Like

Thank you. I will try this too. But my data is correct. I will have multiple sales amount for same day and same category. I need to sum them up based on date and category