IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Query regarding the group by


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
bipinpeter

Active User


Joined: 18 Jun 2007
Posts: 213
Location: Cochin/Kerala/India

PostPosted: Thu Apr 09, 2009 10:48 am
Reply with quote

Hi All,
Please check the below query,

SELECT SUBSTR(A.NAME,1,3) AS NAME
FROM TABLE1 A
GROUP BY NAME;

As per my understating the group by will take input as the output of substring that means group by will take data of 3 bytes.And if there is more than one rows are with same data in input after the group by we will have only one row.


suppose my input as follows,


ANIL
ANITHA
ANISH
ANITA

what will be the output?
As per my understanding i should get only one row with data ANI,but i got 4 rows with ANI in output,why is this?

Bipin
Back to top
View user's profile Send private message
Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Thu Apr 09, 2009 11:25 am
Reply with quote

Hi Bipin,
The result is correct as per your query. You need to use the following query for the desired output.

Code:
SELECT SUBSTR(A.NAME,1,3) AS NAME
FROM TABLE1 A
GROUP BY SUBSTR(A.NAME,1,3);
Back to top
View user's profile Send private message
bipinpeter

Active User


Joined: 18 Jun 2007
Posts: 213
Location: Cochin/Kerala/India

PostPosted: Thu Apr 09, 2009 11:45 am
Reply with quote

Thanks Srihari its working fine...
Back to top
View user's profile Send private message
bipinpeter

Active User


Joined: 18 Jun 2007
Posts: 213
Location: Cochin/Kerala/India

PostPosted: Thu Apr 09, 2009 11:47 am
Reply with quote

One more doubt if we give SELECT SUBSTR(A.NAME,1,3) AS NAME

ideally the output of SUBSTR will come in NAME field,then why the GROUP BY is not working for NAME???

Bipin
Back to top
View user's profile Send private message
Srihari Gonugunta

Active User


Joined: 14 Sep 2007
Posts: 295
Location: Singapore

PostPosted: Thu Apr 09, 2009 2:32 pm
Reply with quote

Bipin,
When you group by NAME, all the four names are different
ANIL
ANITHA
ANISH
ANITA

So you are getting all of them in the result.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts RC query -Time column CA Products 3
No new posts Dynamically pass table name to a sele... DB2 2
No new posts Query on edit primary command CLIST & REXX 5
No new posts Query on edit primary command CLIST & REXX 1
No new posts Compare latest 2 rows of a table usin... DB2 1
Search our Forums:

Back to Top