I understand the point of GROUP BY x.
But how does GROUP BY x, y work, and what does it mean?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Group By Xmeans put all those with the same value for X in the one group.Group By X, Ymeans put all those with the same values for both X and Y in the one group.To illustrate using an example, let’s say we have the following table, to do with who is attending what subject at a university:
When you use a
group byon the subject column only; say:You will get something like:
…because there are 5 entries for ITB001, and 2 for MKB114
If we were to
group bytwo columns:we would get this:
This is because, when we group by two columns, it is saying "Group them so that all of those with the same Subject and Semester are in the same group, and then calculate all the aggregate functions (Count, Sum, Average, etc.) for each of those groups". In this example, this is demonstrated by the fact that, when we count them, there are three people doing ITB001 in semester 1, and two doing it in semester 2. Both of the people doing MKB114 are in semester 1, so there is no row for semester 2 (no data fits into the group "MKB114, Semester 2")
Hopefully that makes sense.