custom SQL QUERY: FINDING TOTAL SALES FOR DISTINCT BRAND AS ATTACHED IN THE XLS #40964
Replies: 1 comment
-
|
Hi @partha-smsrc! I'm Dosu and I'm helping the Apache Superset team. This is a common question — in fact, an almost identical one was asked previously [1]. Here's the situation: The core challenge: Superset's custom SQL metric field accepts aggregate expressions (like What won't work:
Recommended approach — Virtual Dataset: The most reliable solution is to create a virtual (SQL) dataset in SQL Lab, then build your chart on top of it: SELECT Brand, SUM(Sales) AS Total_Sales
FROM your_table
GROUP BY BrandThis gives you one row per distinct brand with its total sales. You can then use a Table chart with subtotals enabled to get the grand total, or add a simple Alternative — Use dimensions in the chart: If you add Both approaches avoid needing to hardcode any brand names and work dynamically regardless of how many brands appear multiple times in your data. To reply, just mention @dosu. Docs are dead. Just use Dosu. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a dataset containing two columns: Brand and Sales. One of the brands, Metformin, appears multiple times, and some sales values (such as 50 and 30) are also repeated.
I want to create a Total column that calculates the sum of sales considering only distinct brands. For example, given sales values of 40, 50, 50, 30, and 30, the total should be 200. However, since Metformin appears more than once, it should only be counted once when calculating this total.
Additionally, I will be writing this logic using custom SQL at the chart level, not directly on the database, so I cannot use a full query structure like SELECT * FROM ....
Dataset (1).xlsx
@dosu
Beta Was this translation helpful? Give feedback.
All reactions