GitHub user dosubot[bot] added a comment to the discussion: custom SQL QUERY: FINDING TOTAL SALES FOR DISTINCT BRAND AS ATTACHED IN THE XLS
<!-- Answer --> Hi @partha-smsrc! I'm [Dosu](https://go.dosu.dev/dosubot) and I'm helping the Apache Superset team. This is a common question — in fact, [an almost identical one was asked previously](https://github.com/apache/superset/discussions/40045#discussioncomment-16889155) [[1]](https://github.com/apache/superset/discussions/40045#discussioncomment-16889155). Here's the situation: **The core challenge:** Superset's custom SQL metric field accepts aggregate expressions (like `SUM(...)`, `COUNT(...)`, etc.) but does **not** allow full `SELECT ... FROM ...` queries. Subqueries are also blocked by default [[2]](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/models/helpers.py#L127-L134). This makes "sum of sales per distinct brand" tricky to express in a single custom SQL metric. **What won't work:** - `SUM(DISTINCT Sales)` — this sums distinct *sales values*, not distinct brands. If two different brands both have a sale of 50, it would only count 50 once, which isn't what you want. **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: ```sql SELECT Brand, SUM(Sales) AS Total_Sales FROM your_table GROUP BY Brand ``` This 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 `SUM(Total_Sales)` metric on this virtual dataset. **Alternative — Use dimensions in the chart:** If you add `Brand` as a **dimension (Group By)** in your chart configuration and use `SUM(Sales)` as the metric, Superset will automatically group by brand (deduplicating it). You'll get one row per brand. Enable **Show Totals** (available in Table charts) to display the overall sum across all distinct brands. Both approaches avoid needing to hardcode any brand names and work dynamically regardless of how many brands appear multiple times in your data. <!-- 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).* --- Docs are dead. Just use [Dosu](https://dosu.dev?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-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=static-docs-feedback&utm_term=apache%2Fsuperset&message_id=2e346c4b-8e1e-4c0d-982d-c877f7a4a2ab) [](https://github.dosu.com/apache/superset?utm_source=github&utm_medium=bot-comment&utm_campaign=github-comment-footer-20260415&utm_content=static-docs-ask-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=static-docs-share-team&utm_term=apache%2Fsuperset) GitHub link: https://github.com/apache/superset/discussions/40964#discussioncomment-17259718 ---- 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]
