distinct-group-by
What it does
Section titled “What it does”Reports SELECT DISTINCT in a query that has GROUP BY.
Why it matters
Section titled “Why it matters”When the select list has all the grouping columns, the rows are distinct already and DISTINCT does nothing. When it leaves one out (SELECT DISTINCT a ... GROUP BY a, b), DISTINCT does change the result, and the grouping by the other column is probably a mistake.
To fix, remove DISTINCT, or group by the selected columns only.
Example
Section titled “Example”...select a from t where a is null and b is not null and c = 'null';select distinct a from t group by a;select distinct a from t;select a from t group by a;...Configuration
Section titled “Configuration”sql: lint: rules: distinct-group-by: warn # off, warn (default), errorEnabled by the presets: sql/sqlfluff.
Suppress one line with sudsy-disable-next-line distinct-group-by in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| sqlfluff | AM01 |
full |