Welcome to TDBSoverflow, Our class's own StackOverflow. Our rules:
  1. Use only meaningful and self-explanatory titles
  2. Tag your questions with meaningful keywords
  3. Use upvotes and downvotes to rate the answers
  4. When you receive a satisfying answer - Click the "V" button
Remember: you may get up to 5 bonus points to your final grade!

group by for distinct purpose

+3 votes
46 views

There are situations where we want to distinct by only part of the attributes in the select. On those situation we don't care which values will be on the other attributes.

We found online that the way to do it is to group by the attributes we want to distinct by.

Is it a good way? and if not, can you suggest better way (a simple one and not lots of nested queries - it is not efficient)

Thanks!

asked Jan 18, 2018 by talsolomon (1,030 points)
If you don't care about the other attributes, why not simply use:
SELECT DISTINCT atr1, atr2, atr3

while not specifying the unwanted attributes?
obviously they are not unwanted!!!
i just don't care which one of them will be chosen. just want one of them

1 Answer

–1 vote
Hey Tall!

Sounds indeed like the best way to achieve what you're looking for is by using Group By as you suggested. Just use a proper aggregation function for the columns that you don't really care for, as you mentioned. The Max() function would perfectly fit for the job.

Regarding performance - it doesn't sound like there are any special performance concerns here - make sure you have the proper indexes on this table. Indexes on the columns you Group-By and filter on may improve performance in case you have any issues there.

Alternative solutions would include joining tables or adding nested queries, and it doesn't sound like that is what you need or looking for :)

Good luck! :)
answered Jan 18, 2018 by babygirl55 (10,420 points)
edited Jan 18, 2018 by babygirl55
your answer isn't brelevant and don't realy answer my question....
You asked if Group By is a good way to achieve what you're looking for. If that's not what you meant, maybe you should clarify your question. And also - be nice to people who try to help you :)
1) babygirl55 is right, please be nice to other studnets trying to help you
2) As I mention before we only answer explicit questions. Your question, i.e.  "Is it a good way..."? is equivalent to "This is my answer for Q1 in HW2, is it correct?" and by the course rules I can't answer that.
3) Rephrase your question and give an example if necessary. Help us help you...
...