Geeks Talk

Prepare for your Next Interview




Group by and order by

This is a discussion on Group by and order by within the SQL Server forums, part of the Databases category; Hi friends, i am executing this query in sql server 2005 select * from users where (primaryemail like 'brijesh%@efextra.com') order by firstname and it's working well.but when ...


Go Back   Geeks Talk > Databases > SQL Server

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 01-22-2008
Moderator
 
Join Date: Sep 2006
Location: Delhi (India)
Posts: 859
Thanks: 12
Thanked 78 Times in 64 Posts
jainbrijesh is on a distinguished road
Group by and order by

Hi friends, i am executing this query in sql server 2005 select * from users where (primaryemail like 'brijesh%@efextra.com') order by firstname and it's working well.but when i tried this one select * from users where (primaryemail like 'brijesh%@efextra.com') group by firstname it's shows me error, why? i am not getting the reason
__________________
Brijesh Jain
brijesh.tester@yahoo.co.in
http://softwaretestingexpertise.blogspot.com
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-22-2008
Expert Member
 
Join Date: Apr 2007
Posts: 404
Thanks: 13
Thanked 52 Times in 52 Posts
susarlasireesha is on a distinguished road
Re: Group by and order by

the GROUP BY clause does not contain all the expressions in the SELECT clause.
Reply With Quote
  #3 (permalink)  
Old 01-22-2008
Expert Member
 
Join Date: Oct 2007
Location: Mumbai
Posts: 344
Thanks: 4
Thanked 52 Times in 43 Posts
bizzzzzare is on a distinguished road
Re: Group by and order by

Hi Brijesh,

To use a Group BY clause, you would need to have atleast one aggregate function in the Select Clause.

Aggregate function like Count(*), Sum, Avg etc.

All other columns excepting the one with the Aggregate function, would need to be part of the GroupBy clause.

Example.

Select METHOD_NAME, Count(*), ROUND(AVG(LAST_CALL_DURATION)/1000,3), SERVICE_NAME, RESPONSE_STATUS
From SGC.GATE_QUEUE
where SYSTEM_NAME in ('CCTOOL', 'FALLOUT_HANDLING_TOOL') -- application of interest
and date_modified >= to_date('10/03/2007 06:24 AM', 'MM/DD/YYYY HH:MI AM')
and date_modified <= to_date('10/03/2007 07:45 AM', 'MM/DD/YYYY HH:MI AM')
and METHOD_NAME = 'getPort25'
Group By SERVICE_NAME, METHOD_NAME, RESPONSE_STATUS

Cheers...
__________________
Regards,

V.Umesh Krishnan
QA Consultant
Reply With Quote
  #4 (permalink)  
Old 01-22-2008
Contributing Member
 
Join Date: Dec 2007
Location: INDIA
Posts: 32
Thanks: 1
Thanked 6 Times in 6 Posts
cme_prak is on a distinguished road
Re: Group by and order by

Hi,

In Group by clause, its dont accept the text datatype. Pls chk whether ur one of the column may be a text.

Thx
Prakash
Reply With Quote
  #5 (permalink)  
Old 01-22-2008
Moderator
 
Join Date: Sep 2006
Location: Delhi (India)
Posts: 859
Thanks: 12
Thanked 78 Times in 64 Posts
jainbrijesh is on a distinguished road
Re: Group by and order by

Quote:
Originally Posted by bizzzzzare View Post
Hi Brijesh,

To use a Group BY clause, you would need to have atleast one aggregate function in the Select Clause.

Aggregate function like Count(*), Sum, Avg etc.


Cheers...
Thanks bizzzzzare,

I got my answer.It's not required to use a Group BY clause, you would need to have atleast one aggregate function in the Select Clause.

But the case is that, the column name which you are using in select should be either in group by statement or in aggregate function.
__________________
Brijesh Jain
brijesh.tester@yahoo.co.in
http://softwaretestingexpertise.blogspot.com
Reply With Quote
Reply

  Geeks Talk > Databases > SQL Server


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
sales order conversion syedrafi123 Oracle Apps 5 11-22-2007 08:20 AM
I am not able to see / add the Sales order number ritz206 Oracle Apps 1 08-10-2007 04:11 PM
difference b/w "group by" and "order by" ? jainbrijesh SQL 9 07-07-2007 01:40 AM
How to make a P.O (Purchase Order) javad.mohammed Data Warehousing 0 04-20-2007 05:16 AM
Is Order by on Rownum Possible Bessie SQL 1 08-17-2006 09:35 AM


All times are GMT -4. The time now is 11:38 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved