| Author |
Topic |
|
djhughes1
Starting Member
4 Posts |
Posted - 2009-11-05 : 15:25:39
|
New to SQL and trying to figure out grouping. So far I have a few questions. When I look at an example from w3schools I see this.SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_name So by looking at this example, my GROUP BY column name has to match the SELECT column name. Is that always the case? Can I group by another column within the same table?Do I have to have a aggregate_function such as sum? What if I am just trying to group by a certain column?For example what I am trying to do is this - Display employee name and group them by title.SELECT Last_NameFROM EmployeeGROUP BY Title When I try this I get this error - Column 'Employee.Last_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.Thanks |
|
|
masterdineen
Aged Yak Warrior
550 Posts |
Posted - 2009-11-05 : 15:37:35
|
| Hello djhughesyes all columns you want to have a group by need to anaggregate functionSELECT column_name, sum(price), count(quantity)from tablewhere clauseGroup by priceGroup by countorder by price ascdoes that make senseMCTS certified |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-11-05 : 15:40:41
|
That doesn't really make sense. I think for your example you'd just what to ORDER the results since you are not applying an aggregate function. That would sort of "group" them in order in your result set. For example:SELECT Title, Last_NameFROM EmployeeORDER BY Title, Last_Name But, if you wanted to count the number of Employee's by Title, then you'd do something like:SELECT Title, COUNT(Title) AS TitleCountFROM EmployeeGROUP BY Title Does that make sense? |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2009-11-05 : 15:41:40
|
| Group by clause has to have every column in your select clause that you're not aggregating (summing,counting,etc.).SELECT Title ,Last_NameFROM EmployeeGROUP BY Title ,Last_NameORDER BY Titlemakes sense.JimEveryday I learn something that somebody else already knew |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-11-05 : 15:45:43
|
quote: Originally posted by masterdineen Hello djhughesyes all columns you want to have a group by need to anaggregate functionSELECT column_name, sum(price), count(quantity)from tablewhere clauseGroup by priceGroup by countorder by price asc
Isn't that the exact opposite of what needs to be done? You group by the columns that do not have aggregate functions applied to them. No need (depending on the requirement) to group by columns contained within an aggregate function. |
 |
|
|
djhughes1
Starting Member
4 Posts |
Posted - 2009-11-05 : 15:57:45
|
quote: Originally posted by jimf Group by clause has to have every column in your select clause that you're not aggregating (summing,counting,etc.).SELECT Title ,Last_NameFROM EmployeeGROUP BY Title ,Last_NameORDER BY Title
Having all the columns in the group by clause that were in the select clause is what I was not understanding. So there is no way use the GROUP BY clause with a single column if you have multiple columns within your SELECT?Thanks for all the quick responses by the way. All of them have been helpful. |
 |
|
|
djhughes1
Starting Member
4 Posts |
Posted - 2009-11-05 : 15:59:24
|
quote: Originally posted by Lamprey That doesn't really make sense. I think for your example you'd just what to ORDER the results since you are not applying an aggregate function. That would sort of "group" them in order in your result set. For example:SELECT Title, Last_NameFROM EmployeeORDER BY Title, Last_Name But, if you wanted to count the number of Employee's by Title, then you'd do something like:SELECT Title, COUNT(Title) AS TitleCountFROM EmployeeGROUP BY Title Does that make sense?
It does make sense. I had used the ORDER BY before but was still having problems with the GROUP BY. Both your example you listed are good and helped me out. |
 |
|
|
masterdineen
Aged Yak Warrior
550 Posts |
Posted - 2009-11-05 : 16:12:59
|
| sorry i didnt explain myself properly. Didnt mean to confuse you. sorry. |
 |
|
|
djhughes1
Starting Member
4 Posts |
Posted - 2009-11-05 : 16:19:24
|
SO if you want to further break down the query after it has been grouped you have to use the HAVING Clause correct? For example, you have the Title, Last_Name, and also a Wage column. I would need to add the wage intot he querySO it would be something like SELECT Title ,Last_Name, WageFROM EmployeeGROUP BY Title ,Last_Name, WageHAVING aggregate_function(column_name) operator value So in this instance as well, if I do not need the aggregate piece, then I would just need to use the column name?edit - Let me try to rephrase that as that sounds confusing. Can you group data from one column within another. For example, group by wage within the job title on the query?edit - I think I got it figured out. SELECT Wage, Last_Name, TitleFROM EmployeeGROUP BY Wage, Last_Name, TitleOrder by Title So that will display a query that has the data grouped by Wage and then within that it is ORDERed by the Title.Does that seem correct? |
 |
|
|
masterdineen
Aged Yak Warrior
550 Posts |
Posted - 2009-11-06 : 03:50:02
|
| you learn something new everyday on this site.One day i will be excellent on here. lolMCTS certified |
 |
|
|
|