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 Why TS/OP would Not contact site supp... RahulG31 All Other Mainframe Topics 2 Wed Mar 22, 2017 7:46 am
No new posts JCL to Set Return code based on DB2 S... vinu78 DB2 17 Mon Mar 13, 2017 9:47 pm
No new posts SQL query not working in Cobol program. CuriousMainframer COBOL Programming 14 Wed Feb 22, 2017 5:56 pm
No new posts Omegamon for DB2 query ashek15 DB2 0 Wed Feb 15, 2017 11:25 am
No new posts Query on DB2 WLM stored procedures ashek15 DB2 8 Sun Feb 12, 2017 11:13 pm


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