Custom Sql query Problem #40045
Replies: 1 comment 2 replies
-
|
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 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: 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:
SUM(DISTINCT Sales)
If what you actually want is the sum of one representative sale per distinct brand, you'd need a different approach:
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]. 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? To reply, just mention @dosu. Share context across your team and agents. Try Dosu. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
which add up to 200. Since Metformin is repeated twice, it should be considered only once.
Dataset.xlsx
@dosu
Beta Was this translation helpful? Give feedback.
All reactions