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: 785
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: 1278
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 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 SQL query to run through list of valu... Ni3-db2 DB2 14 Wed Dec 14, 2016 9:52 am
This topic is locked: you cannot edit posts or make replies. Db2 SQL query to convert rows into co... subratarec DB2 5 Thu Nov 17, 2016 4:51 pm


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