A Dolegowski
A Dolegowski

Reputation: 93

MySQL Distinct guidance needed

My Goal: Display the names of the top 10 users who have submitted the maximum number of revision in one changeid. (A change can have multiple revisions)

Select DISTINCT(rev_authorUsername), COUNT(rev_id) as revnum FROM t_revision
group by rev_authorUsername, rev_changeId
 order by revnum desc
 limit 10;

I get the top 10 authors but I get duplicate names. Any help pls??

Upvotes: 0

Views: 42

Answers (2)

freezed
freezed

Reputation: 1339

  1. DISTINCT modifiers applys for the selected set, do not use parenthesis around rev_authorUsername
  2. Using DISTINCT and GROUP_BY (here with different columns) may have some unexpected result:

(…) cavalier treatment of "Group By" and "Distinct" could lead to some pernicious gotcha's down the line if you're not careful.

What about this request:

SELECT DISTINCT rev_authorUsername, COUNT(rev_id) as revnum
FROM t_revision
ORDER BY revnum DESC
LIMIT 10;

(The use of DISTINCT & GROUP BY is explained in the MySQL Reference Manual - SELECT Syntax.)

Upvotes: 1

marvinscham
marvinscham

Reputation: 42

Try removing the brackets surrounding rev_authorUsername after DISTINCT, they shouldn't be there and might be what's messing with your results.

Upvotes: 0

Related Questions