GitHub user dosubot[bot] added a comment to the discussion: Custom Sql query Problem
<!-- Answer --> Hi @Aruneem, you can achieve this using a custom SQL metric in the chart editor. Since you want the sum of sales for **distinct brands** (treating repeated brands as one), you can use a combination of `SUM` and `CASE` or leverage `COUNT(DISTINCT ...)` logic. Here's an approach using custom SQL in the metric field: **To get the total sales considering each brand only once (i.e., sum of sales per distinct brand):** You'd write a custom SQL metric like: ```sql SUM(CASE WHEN Brand = 'Metformin' THEN Sales ELSE 0 END) ``` However, since your goal is to sum sales for *distinct brand occurrences* across the dataset (essentially deduplicating repeated rows), the cleanest approach at the chart level is: 1. **Create a custom SQL metric** in the Metrics section of the chart editor using: ```sql SUM(DISTINCT Sales) ``` ⚠️ **Important caveat**: `SUM(DISTINCT Sales)` sums distinct *sales values*, not distinct brands. In your example, if Metformin has sales of 50, 50, 30, 30, 40, then `SUM(DISTINCT Sales)` would give 50 + 30 + 40 = 120, which removes duplicate *values* regardless of brand. If what you actually want is the **sum of one representative sale per distinct brand**, you'd need a different approach: 2. **Group by Brand first** — In the chart configuration, add `Brand` as a dimension/group-by column and use `SUM(Sales)` as the metric. This gives you total sales per brand. Then to get an overall total, you can use a post-processing step or a Table chart with subtotals enabled. 3. **Use a derived virtual dataset** — Create a new virtual dataset in SQL Lab with: ```sql SELECT Brand, SUM(Sales) as Total_Sales FROM your_table GROUP BY Brand ``` Then build your chart on top of this virtual dataset. Option 3 (virtual dataset) is typically the most reliable way to handle deduplication logic that doesn't fit neatly into a single aggregate expression [[1]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/models/helpers.py). Could you clarify exactly which rows should be deduplicated? For instance, should all rows for "Metformin" collapse into one sum, or should only exact duplicate rows (same Brand + same Sales) be removed? <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=reply-with-mention&utm_term=apache%2Fsuperset).* --- Share context across your team and agents. Try [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-tagline&utm_term=apache%2Fsuperset). [](https://app.dosu.dev/response-feedback?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-feedback&utm_term=apache%2Fsuperset&message_id=da57e567-b29a-47b7-9fd8-6c1861f0727c) [](https://github.dosu.com/apache/superset?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-learn-repo&utm_term=apache%2Fsuperset) [](https://app.dosu.dev/signup?referrer=openSource&source=github-footer&utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=knowledge-infrastructure-add-team&utm_term=apache%2Fsuperset) GitHub link: https://github.com/apache/superset/discussions/40045#discussioncomment-16889155 ---- This is an automatically sent email for [email protected]. To unsubscribe, please send an email to: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
