Not sure how to do this and not sure how to phrase it to find the answer.
Say I have SELECT id, name, title, stuff FROM table
Id, name, and title can be whatever, it doesn’t matter. But I only want to select 1 column where stuff is distinct. So..
1, Bob, Builder, Hammer
2, Tom, Train, Track
3, Charlotte, Spider, Web
1, Bob, Builder, Children
2, Tom, Train, Children
3, Charlotte, Spider, Children
So I want to select the first three and only one of the next three.
you can try this:
we have the aggregation here (group by) and all the fields that is not enumerated in GROUP BY claus should be processed by some aggregate function. MAX is one of these functions. this mean the following. When sql engine finds several rows with the same value of stuff field, it shohuld collapse them to single row. But the other fields has different values. What one of three values should it take, saying, for field Name? We should point sql engine what to do in this case. By specifying aggregate function MAX for instance, we enforce sql engine to select the max of three values. In our case “Tom” will be returned for field Name. We could use some other aggregate function, e.g. MIN. It doesn’t matter in this case.
Edition #2 (more safe):