Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select multiple columns while using a Group By

Author  Topic 

joglez
Starting Member

2 Posts

Posted - 2010-11-15 : 15:03:40
Hi guys,
I want to select multiple columns and group by just one column, But i get the error that the columns aren't being used in the group by or avg clause.. How can I select many columns when using an agregated function and a Group by?? Example:
SELECT table1.column1, table1.column2, AVG(table2.column1) FROM table1, table2 Where table1.column3='yes' Group By table1.column4

NOTICE THAT 'table1.column1 and 'table1.column2' aren't being used in the avg() or the group by

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-11-15 : 15:22:14
If you are grouping by column4 then you will only get one row per distinct column4 value. If there are many rows of underlying data with different values for column1, and column2 then what would expect to see for those columns? There can only be one value so is it a min, max, avg, sum, etc. If you want to show all the values for column1 and column2 then you need to add them to your GROUP BY or remove the Average and the group by entirely.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -