|
|
|
SSChampion
        
Group: General Forum Members
Last Login: Friday, May 18, 2007 3:36 PM
Points: 10,040,
Visits: 1
|
|
| Comments posted to this topic are about the Question of the Day for 30 Nov 2005 posted at http://www.sqlservercentral.com/testcenter/qod.asp?QuestionID=651.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, November 30, 2005 6:29 AM
Points: 2,
Visits: 1
|
|
It seems unlikely that everyone is wrong. Even unlikelier that I am right! but - SELECT COL1,COL2,COL3 FROM TABLE1 GROUP BY COL1 works! However, SELECT COL2,COL3 FROM TABLE1 GROUP BY COL1 does NOT work. So doesn't that mean that "A" is, in fact true? Rav
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, November 30, 2005 6:29 AM
Points: 2,
Visits: 1
|
|
Please Ignore my previous post. Doh! Doh! Doh!
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, July 23, 2007 6:53 AM
Points: 107,
Visits: 1
|
|
Actually A is not fully true. For instance, You can say SELECT col1, col2, max(col3) from TableA GROUP BY col1, col2 Therefore col3 is not technically in the group by clause. I could be wrong. Its early and I have not had my coffee yet. But I think the above is true.  
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Monday, July 23, 2007 6:53 AM
Points: 107,
Visits: 1
|
|
Opps. See it was too early in the morning. All "nonaggregated columns".
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Tuesday, May 21, 2013 7:21 AM
Points: 1,894,
Visits: 221
|
|
Even though I got it correct I don't think it is completely correct.
select col1, col2, "extra" = 'X' from table1 group by col1, col2
would work even though the column "extra" is a non-aggregate and is not in the group by.
|
|
|
|
|
Right there with Babe
      
Group: General Forum Members
Last Login: Thursday, June 09, 2011 10:47 AM
Points: 779,
Visits: 221
|
|
That answer's not 100% accurate.
All nonaggregated columns in the SELECT clause must also be listed in the GROUP BY clause; assuming that there is at least one aggregated column in the SELECT or HAVING clauses.
You don't need to specify a GROUP BY clause if there are no aggregates in your SELECT or HAVING clauses.
But that's just being nitpicky.
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Monday, July 30, 2012 10:42 AM
Points: 3,434,
Visits: 519
|
|
I saw solutions that used Group By without aggregates.
Regards, Yelena Varshal
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 11:13 AM
Points: 6,351,
Visits: 5,369
|
|
All nonaggregated columns in the SELECT clause must also be listed in the GROUP BY clause
If we're nitpicking then....
Even though I got it correct I don't think it is completely correct.
"extra" = 'X' is an expression not a column in the query and therefore is not bound by the above rule. 
I saw solutions that used Group By without aggregates
Yep
SELECT col1 FROM [table] GROUP BY col1
yields the same result as
SELECT DISTINCT col1 FROM [table]
However GROUP BY includes an implicit COUNT(*) if not explicitly defined and therefore technically all GROUP BY's have an aggregate ;)
Far away is close at hand in the images of elsewhere. Anon.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, August 04, 2009 8:25 AM
Points: 159,
Visits: 122
|
|
I got this correct, but only because i had to assume that the "correct" answer wouldnt be totally correct.
Select 1 as "Col1", Col2, Col3 From Table Group By Col2, Col3
...will work, and I would consider 1 a non-aggregate, or is that an incorrect statement.
|
|
|
|