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: 208
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: 208
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: 208
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 Sorting group data rajella DFSORT/ICETOOL 3 Sun Jan 22, 2017 11:32 pm
No new posts Join in SQL Query vickey_dw DB2 2 Tue Jan 17, 2017 12:15 am
This topic is locked: you cannot edit posts or make replies. MQ setup query mohitsaini Java & MQSeries 4 Sun Jan 08, 2017 1:53 am
No new posts DB2 query Using Recursion, Converting... smilewithashu2 DB2 1 Tue Jan 03, 2017 12:50 pm
No new posts Syncsort Help to group fields sudhakarraju SYNCSORT 6 Thu Dec 29, 2016 1:38 am


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