DB2 Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDB2 Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old July 9th, 2004, 12:55 PM
nomorephp nomorephp is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 35 nomorephp User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Where do i put the group by clause?

can someone tell me where i should put the group by clause in this sql statement. i want to group by et.description (it is a varchar (256) ).

thanks a lot

SELECT IP.PAYEE_NAME, E.POST_FILE_ITEM_ID, P.AMOUNT, P.SERIAL_NO, P.ITEM_SEQUENCE_NO, ET.DESCRIPTION
FROM ISSUE_FILE_ITEM_PAYEE IP, EXCEPTION E, POST_FILE_ITEM P, POST_FILE_BANK_ACCOUNT A,
ISSUE_FILE_ITEM IT, EXCEPTION_TYPE ET
WHERE E.POST_FILE_ITEM_ID = P.POST_FILE_ITEM_ID
AND P.PROCESSING_CYCLE_DATE BETWEEN '****-**-**' AND '****-**-**'
AND A.ACCOUNT_NO= '*******'
AND P.POST_FILE_BANK_ACCOUNT_ID = A.POST_FILE_BANK_ACCOUNT_ID
AND P.ISSUE_FILE_ITEM_ID = IP.ISSUE_FILE_ITEM_ID
AND P.ISSUE_FILE_ITEM_ID = IT.ISSUE_FILE_ITEM_ID
AND E.EXCEPTION_TYPE_ID = ET.EXCEPTION_TYPE_ID;

Reply With Quote
  #2  
Old July 9th, 2004, 03:02 PM
dcaillouet's Avatar
dcaillouet dcaillouet is offline
Big Endian
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2001
Location: Fly-over country
Posts: 1,173 dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 16 h 29 m 5 sec
Reputation Power: 24
A GROUP BY clause is used when you have an aggregate function (SUM, COUNT, etc.). It is used when you don't want to retrieve a value associated with a single record but you want to retrieve a value related to a group of records.

Since you don't have any aggregate functions in your query, a GROUP BY clause doesn't accomplish anything. You could add an ORDER BY clause as the last line if you wanted to have all the descriptions in order so identical values would be "grouped" together if that was your intention.

Reply With Quote
  #3  
Old July 10th, 2004, 07:16 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,310 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 4 h 20 m 43 sec
Reputation Power: 888
excellent answer, dcaillouet, which i would like to add a few words to

it is permissible to have a GROUP BY without an aggregate function

(it is also permissible to have an aggregate function without a GROUP BY -- the famous example is "select count(*) from mytable" where the entire table is the group)

a GROUP BY without aggregate functions accomplishes the same as SELECT DISTINCT
__________________
r937.com | rudy.ca

Reply With Quote
  #4  
Old July 10th, 2004, 02:21 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
I believe you can do a GROUP BY with non-aggregate functions if you specify each column you want to group by. In other words, you would have to list all the columns that you have in the SELECT clause in the GROUP BY clause, which means they would not group unless all columns are the same, not just the description field.

And to answer the original question, the GROUP BY clause goes after your WHERE clause.
__________________
~Nemi

Before posting did you try:
[ Javadocs | Google ]

Last edited by Nemi : July 10th, 2004 at 02:36 PM.

Reply With Quote
  #5  
Old April 8th, 2006, 05:16 AM
rohitsamvada rohitsamvada is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Posts: 1 rohitsamvada User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 3 sec
Reputation Power: 0
Quote:
Originally Posted by Nemi
I believe you can do a GROUP BY with non-aggregate functions if you specify each column you want to group by. In other words, you would have to list all the columns that you have in the SELECT clause in the GROUP BY clause, which means they would not group unless all columns are the same, not just the description field.

And to answer the original question, the GROUP BY clause goes after your WHERE clause.


We can over come this limitaion by using "Over-Partition by" command on each of the columns in the select query. though this might reduce the query performance a bit.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDB2 Development > Where do i put the group by clause?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway