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
 

 

Need some help in understanding the query

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
Saikat Sengupta

New User


Joined: 08 Dec 2009
Posts: 13
Location: Mumbai

PostPosted: Mon Jul 30, 2012 5:59 pm    Post subject: Need some help in understanding the query
Reply with quote

I am facing a problem understanding the out put of a DB2 query. I’ll briefly give u the table columns and then the query.
The table is an Employee table with:
1st Col: Dept Num
2nd Col: Employee ID
3rd Col: Employee Last Name
4th Col: Employee First Name
…. The rest of the cols I am not including since the query is restricted to these columns.

I ran the following query 1st:
Code:
Select Dept_Num, Employee_ID, Emp_Lst_Nam, Emp_Fst_Nam
From DXXX.TBWP_EMP
Where Emp_Lst_Nam IN (‘PANCAKE’);

The output was:

Code:
---------+---------+---------+---------+---------+---------+---------+
DEPT_NUM  EMPLOYEE_ID  EMP_LST_NAM      EMP_FST_NAM   
---------+---------+---------+---------+---------+---------+---------+
      14.     90440.   PANCAKE          MEREDITH               
     458.      1641.   PANCAKE          MARY                   
DSNE610I NUMBER OF ROWS DISPLAYED IS 2


I ran the following query next:
Code:
Select Dept_Num, Employee_ID, Emp_Lst_Nam, Emp_Fst_Nam
From DXXX.TBWP_EMP
Where Employee_ID IN ( Select Employee_ID from DXXX.TBWP_EMP
                                           Where Emp_Lst_Nam IN (‘PANCAKE’))

The output was:

Code:
---------+---------+---------+---------+---------+---------+
DEPT_NUM  EMPLOYEE_ID  EMP_LST_NAM      EMP_FST_NAM   
---------+---------+---------+---------+---------+---------+
      14.     90440.   PANCAKE          MEREDITH
     458.      1641.   PANCAKE          MARY     
     881.      1641.   BENTLEY          HILMA                   
    3885.      1641.   WALLACE          SARA     
    4022.      1641.   LEMAHIEU         BARBARA   
    4138.      1641.   LOMELI           ESTHER     
DSNE610I NUMBER OF ROWS DISPLAYED IS 6

I am not getting how is the 2nd query working to produce such result set. Can anyone explain how the 2nd query is working?

Coded - Anuj
Back to top
View user's profile Send private message

Gnanas N

Active Member


Joined: 06 Sep 2007
Posts: 788
Location: Chennai, India

PostPosted: Mon Jul 30, 2012 6:10 pm    Post subject:
Reply with quote

Perhaps, someone inserted some rows after your first query execution, before second query run.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Mon Jul 30, 2012 6:17 pm    Post subject:
Reply with quote

You have multiple employee_id's with different emp_lst_name. employee_id '1641' is used for Bentley, Wallace, Lemahieu, and Lomeli. Your first query is based on emp_lst_name but your second is based on employee_id (which is not unique across dept_num's)
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Mon Jul 30, 2012 9:30 pm    Post subject:
Reply with quote

your first query returns two EmpIds : 90440 and 1641
your second query returns all rows with those empids

maybe what you wanted to write was :
Code:
Select Dept_Num, Employee_ID, Emp_Lst_Nam, Emp_Fst_Nam
From DXXX.TBWP_EMP
Where (dept_num,Employee_ID) IN ( Select Dept_num,Employee_ID from DXXX.TBWP_EMP
                                           Where Emp_Lst_Nam IN (‘PANCAKE’))
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 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