Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

Query regarding the group by

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
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    Post subject: Query regarding the group by
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    Post subject:
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    Post subject:
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    Post subject:
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    Post subject:
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    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Need DB2 query to fetch previous row ! Chandan1993 DB2 10 Sat Jun 03, 2017 10:43 am
No new posts find RACF group for access to spooled... jzhardy JCL & VSAM 1 Mon May 08, 2017 11:46 am
No new posts Group Data based on a key Arun Raj DFSORT/ICETOOL 7 Thu Apr 27, 2017 11:29 pm
No new posts Need Help with : IFTHEN(WHEN=GROUP) Sumanta_89 DFSORT/ICETOOL 1 Wed Apr 19, 2017 5:23 pm
No new posts Group elements using ICETOOL Learncoholic DFSORT/ICETOOL 2 Fri Apr 14, 2017 11:01 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us