Question:
SQL group by Clause please help?
Nick M
2011-11-15 23:49:43 UTC
I woudld like this to work (this is a uni question), asking for the albumtitle and date:

select albumtitle, date
from albums left outer join date
group by albumcode

It needs to be grouped by albumcode (instead of album title since there may be 2 of the same title) but I simply can;t it gives me an error.
Four answers:
TheMadProfessor
2011-11-16 07:48:31 UTC
Whenever you have a GROUP BY, the only things that can appear in the SELECT list are



1) items in the GROUP BY list

2) aggregate functions (MIN, MAX, COUNT, AVG, etc.)

3) constant values



Also, having both a table and column named 'date' is a TERRIBLE idea...avoid using keywords as identifiers like the plague! Drop me a line as to your schema and exactly what you're trying to do and I may be able to make some suggestions.
doug a
2011-11-16 01:46:13 UTC
Of course it will your SQL is lacking stuff



Using the left outer join you need to link them on the field



select a.albumtitle,b.date

from albums

left outer join loans b on a.code = b.code



Where code is the unique ref from a held in b



to begin with...
AJ
2011-11-16 07:47:06 UTC
any field you put in the select clause that is not an aggregate, ie Sum(), Min(), Max(), etc, must be in the group by clause as well.
2011-11-15 23:51:25 UTC
http://www.techonthenet.com/sql/group_by.php


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...