tags:

views:

39

answers:

1

One of the usages of GROUP BY is to group entries where a specified column is of the same value. How can we group entries if 2 or more selected rows have the same value?

+2  A: 

You can GROUP BY Multiple columns (which is what you're asking, I think).

Select column1, column2 from MyTable
GROUP BY column1, column2
Tobiasopdenbrouw
Thank you! On a related issue, if the `SELECT` statement has a `WHERE` clause, should I create an index on all columns used by the `WHERE` *and* the `GROUP BY` clause to increase performance? or just the columns used inside the `WHERE` clause are the one that matters?
banx
Indices on all of those columns will affect performance, though whether they are best served by an index on all, different indices on each, an index on the where case and another on the two used in the group, or an index on the where-used column that "uses" the columns on the group by (in databases with such a concept of indices "using" other columns) will vary according to the data and the most common values for comparing against in the where clause. And that's treating this question in isolation and ignoring the positive and negative effects on other uses of the DB.
Jon Hanna
Have a look here. http://odetocode.com/Articles/237.aspx
Tobiasopdenbrouw