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?
views:
39answers:
1
+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
2010-08-10 09:12:01
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
2010-08-10 09:18:37
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
2010-08-10 09:36:21
Have a look here. http://odetocode.com/Articles/237.aspx
Tobiasopdenbrouw
2010-08-10 09:36:47