Click here to Skip to main content
15,887,936 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I'm trying to GROUP BY in a SQL statement using Query Builder in VS 2015, but it's not working... Can you please tell me what's wrong? Thank you! :)

What I have tried:

SQL
SELECT students_details.class AS Class, students.name AS Name, students.guardian AS Gurdian, students.phone AS Phone, students.mobile AS Mobile
FROM students_det INNER JOIN students ON students_details.studentID = students.studentID
ORDER BY Class
GROUP BY Class
Posted
Updated 11-Oct-16 20:48pm
Comments
Herman<T>.Instance 12-Oct-16 2:43am    
ORDER BY is allways the last sentence of a query.
See here or here
JC Carmo 12-Oct-16 2:47am    
I've already tried putting ORDER BY in the end, but still doesn't work...
Herman<T>.Instance 12-Oct-16 2:49am    
Not working with which error message?
Herman<T>.Instance 12-Oct-16 2:50am    
GROUP BY students_details.class

GROUP BY does not working on the alias name

1 solution

First of all, GROUP BY should appear before ORDER BY clause. ORDER BY should be the last clause in your SELECT query.

You haven't used any aggregate function for which GROUP BY is required.
If you want to unique records in a result set use DISTINCT clause with ORDER BY if required.

Just remove/ comment out the GROUP BY and it should work fine. You can add a DISTINCT tag if you need.

SQL
SELECT DISTINCT students_details.class AS Class, students.name AS Name, students.guardian AS Gurdian, students.phone AS Phone, students.mobile AS Mobile
FROM students_det INNER JOIN students ON students_details.studentID = students.studentID
--GROUP BY students_details.class, students.name, students.guardian, students.phone, students.mobile
ORDER BY students_details.class


Hope, it helps :)
 
Share this answer
 
v3
Comments
JC Carmo 12-Oct-16 2:52am    
Thank you Suvendu! What I am trying to do is to group all students by Class, for example: Classroom 1 will group a list of all its students, Class 2... and so forth.
Suvendu Shekhar Giri 12-Oct-16 3:06am    
Ok.. in that case just the ORDER BY students_details.class should do the job :)
JC Carmo 12-Oct-16 3:07am    
OK, I'm going to try that. Thank you once again for your kind help! :)
Wendelius 16-Oct-16 3:36am    
Nice answer, a 5.
Suvendu Shekhar Giri 16-Oct-16 11:33am    
Thanks Mika :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900