Home      Mainframe Forum      Mainfarme Tutorials      IBM Manuals      Mainframe Interview Questions      Mainframe Books      IT News     SiteMap     Downloads


     
 
MAINFRAME - TIP OF THE DAY : Q. If there is a situation, where we need to code more than 255 steps in a JOB? A. We need to split jcl into two jcls , at the end of the first jcl check the condition code and initiate the second jcl.

Google
 
Web mainframegurukul.com

Programmers Voted for below topics.      Please Vote for good Posts. Votes Salutes
Topic Title Votes Salutes
HOW TO FIND LINKAGE LENGTH 6
SOC 7 abend - interview question 6
splitting of records using cobol 6
EJECT Verb in COBOL 5
jcl sort to add leading zeroes 5
Infosys Training Program for freshers 5
Changes in copybook 4
Comparing two files 3
What are the issues related with correlated subqueries? 3
VSAM File status 23 ,but records are present in the file 3
what is index or subscript in cobol. please give me brief ex 3
IKJ56500I COMMAND DSN NOT FOUND 3
Cobol Multiple Choice Questions By Shravan Wipro 3
Cobol Multiple Choice Questions By Shravan Wipro 1 1
Hexadecimal to decimal conversion - SORT JCL INREC OUTREC 1 1
What is the difference between SYNONYM and ALIAS? 1 1
PROCLIB 1 1
sort two files with same sort condition 1 1
What is the difference between Copy and Include? 1 1
EZtrv+ - Is there a way to suppress ez-code from printing... 1 1
Merging two files into one based on certain key fields: 1 1
what is index or subscript in cobol. please give me brief ex 1 1
Copy GDG to GDG (one to one). 1 1
jcl sort to add leading zeroes 5
VSAM File status 23 ,but records are present in the file 3
Urgent Help needed !! 3
Hexadecimal to decimal conversion - SORT JCL INREC OUTREC 3
How we can access PDS and its Members thru COBOL ? 3
Cobol Multiple Choice Questions By Shravan Wipro 2
SOC 7 abend - interview question 2
How to read HEX file and convert it into decimal in REXX. 2
Various Locking mechanism for cursors and other SQL? 2
How to fetch and compare the system date in JCL 2
stored procedure in cobol 2
VERTICAL ARRAY IN CICS 2
what s the difference between DB2 & ADB2? 2
What is auditing? 2
Reg DFSORT utility. 2
Computer based tutorial on IMS DB/DC 2
what is index or subscript in cobol. please give me brief ex 2
How to compare two ps files having millions of records ? 2
How to convert Julian date format into yyyymmdd date format? 1
What is the difference between Static and Dynamic Binding? 1
In jcl please help me. 1
Access DB2 region using rexx 1
DFHAC2016 - Trans XXXX cannot run because program XXX is Not 1
EZtrv+ - Is there a way to suppress ez-code from printing... 1
How to use XREF.. 1
DB2 checkpoint-restart doubts 1
Comparing 2 files based on Acct number without using tables 1
JCL-I need to FTP from MF to Local computer 1
cics 1
sort two files with same sort condition 1
U4093 IMS Abend 1
writing 2 records for each qualifying input record. 1
Is it possible to get last modified date of PDS members... 1
Merging two files into one based on certain key fields: 1
How to run only 2 steps out of 100 steps??? 1
Corresponding statement 1
Difference between an Object Module and Load Module 1
Moving cursor to next field in ISPF dialog 1
DB2 query !! 1
JCL Interpreter :?: 1
Comparing Date with current date 1
SORT verb - help 1
How to assign System Date/Time to a parameter in a JCL pgm? 1
is the answer correct?? 1
Mainframe Developer Requirement - Coimbatore 1
Man of the year 1
I need to FTP from MF to UNIX 1
Control flow from one STEP to another 1
Query on SQL

 
Post new topic   Reply to topic    mainframegurukul.com Forum Index -> DB2 SQL - DB2 PROGRAMMING
  View previous topic :: View next topic  
Author Message
bolastuff1975
Member


Joined: 21 Sep 2007
Posts: 8

Answer was approved by Question owner.
Useful Post
0 votes
Not an useful post
Greate Post!! Salute Author
0 salutes

PostPosted: Sat Sep 22, 2007 12:04 am    Post subject: Query on SQL Reply with quote

bolastuff1975@yahoo.co.uk

Hi Please, help me out in solving this issue.

i need to update field2 & field3 of table-1 when there are more than 2 rows with same key-2 and field1.
field2 & field3 are picked from table2 based on key-1 and max time stamp for t1.

Table structre and data is shown below. i have also shown the sql where i am not able to get a unique row.

please, note bothe the tables contain differnet fields for update, i have not listed them b'cos they do not form a part of key.


Table1
-------
Key1 char
Key2 char
field1
field2
field3

data in table1
----------------

key1 key2 fiedl1 field2 field3
----- ----- ------ ------- ------
101 54 1001 blabla blabla
102 54 1001 blabla blabla
103 54 1001 blabla blabla
104 54 1001 blabla blabla
105 55 1001 blabla blabla
106 50 1002 blabla blabla

table2
-------
key1
field1
field2
field3
timestamp

Date in table2
----------------

key1 field1 field2 field3 timestamp
101 1001 abc abc t1
101 1001 abc abc t2
102 1001 abc abc t3
102 1001 abc abc t31
103 1001 abc abc t4
104 1001 abc abc t5

sql

update table1
set (b.field3,b.field4)
=(select field1,field2)
from table1 a,table-2 b
where a.key1 = b.key1
and timestamp = (select max(ts) from table2 c
where c.key1 = b.key1)
)
where a.key1=?? i need to retreive a unique key.

i tried the below inner query but resulted in more than 1 row

where a.key1 = (select c.key1 from table1 c
where c.field1 = a.field1
and c.key2 in ( select key2 from table1 d
where d.key1 = c.key1
and field1 = c.field1
group by key2
having count(*) > 1))
Back to top
View user's profile Send private message
Click here, If this post answer your question. <-- Click on right mark icon. If this post answer your question.

DavidatK
Active Member


Joined: 27 Mar 2007
Posts: 65
Location: Troy, MI USA

Answer was approved by Question owner.
Useful Post
0 votes
Not an useful post
Greate Post!! Salute Author
0 salutes

PostPosted: Sat Sep 22, 2007 5:47 am    Post subject: Reply with quote

bolastuff1975,

Try this SQL

Code:

DECLARE GLOBAL TEMPORARY TABLE TABLE1         
       (KEY1            CHAR(3)               
       ,KEY2            CHAR(2)               
       ,FIELD1          CHAR(4)               
       ,FIELD2          CHAR(6)               
       ,FIELD3          CHAR(6)               
        )                                     
;                                             
INSERT INTO SESSION.TABLE1 (KEY1, KEY2, FIELD1, FIELD2, FIELD3)
       VALUES('101', '54', '1001', 'BLABLA', 'BLABLA');         
INSERT INTO SESSION.TABLE1 (KEY1, KEY2, FIELD1, FIELD2, FIELD3)
       VALUES('102', '54', '1001', 'BLABLA', 'BLABLA');         
INSERT INTO SESSION.TABLE1 (KEY1, KEY2, FIELD1, FIELD2, FIELD3)
       VALUES('103', '54', '1001', 'BLABLA', 'BLABLA');         
INSERT INTO SESSION.TABLE1 (KEY1, KEY2, FIELD1, FIELD2, FIELD3)
       VALUES('104', '54', '1001', 'BLABLA', 'BLABLA');         
INSERT INTO SESSION.TABLE1 (KEY1, KEY2, FIELD1, FIELD2, FIELD3)
       VALUES('105', '55', '1001', 'BLABLA', 'BLABLA');         
INSERT INTO SESSION.TABLE1 (KEY1, KEY2, FIELD1, FIELD2, FIELD3)
       VALUES('104', '50', '1002', 'BLABLA', 'BLABLA');         
DECLARE GLOBAL TEMPORARY TABLE TABLE2       
       (KEY1            CHAR(3)             
       ,FIELD1          CHAR(4)             
       ,FIELD2          CHAR(6)             
       ,FIELD3          CHAR(6)             
       ,TS              CHAR(5)             
        )                                   
;                                           
INSERT INTO SESSION.TABLE2 (KEY1, FIELD1, FIELD2, FIELD3, TS) 
       VALUES('101', '1001', 'A11', 'A22', 'T1   ');           
INSERT INTO SESSION.TABLE2 (KEY1, FIELD1, FIELD2, FIELD3, TS) 
       VALUES('101', '1001', 'B11', 'B22', 'T2   ');           
INSERT INTO SESSION.TABLE2 (KEY1, FIELD1, FIELD2, FIELD3, TS) 
       VALUES('102', '1001', 'C11', 'C22', 'T3   ');           
INSERT INTO SESSION.TABLE2 (KEY1, FIELD1, FIELD2, FIELD3, TS) 
       VALUES('102', '1001', 'D11', 'D22', 'T31  ');           
INSERT INTO SESSION.TABLE2 (KEY1, FIELD1, FIELD2, FIELD3, TS) 
       VALUES('103', '1001', 'E11', 'E22', 'T4   ');           
INSERT INTO SESSION.TABLE2 (KEY1, FIELD1, FIELD2, FIELD3, TS) 
       VALUES('104', '1001', 'F11', 'F22', 'T5   ');           
SELECT *               
  FROM SESSION.TABLE1 
;                     
SELECT *               
  FROM SESSION.TABLE2 
;                     
UPDATE SESSION.TABLE1             T1               
   SET (FIELD2,FIELD3) =                           
       (SELECT FIELD2,FIELD3                       
          FROM SESSION.TABLE2     T2               
         WHERE T2.KEY1          = T1.KEY1           
           AND T2.TS =                             
               (SELECT MAX(TS)                     
                  FROM SESSION.TABLE2      T2A     
                 WHERE T2A.KEY1 = T1.KEY1           
                )                                   
        )                                           
 WHERE EXISTS                                       
       (SELECT COUNT(T1A.KEY2)                     
          FROM SESSION.TABLE1              T1A     
         WHERE T1A.KEY2         = T1.KEY2           
           AND T1A.FIELD1       = T1.FIELD1         
        HAVING COUNT(T1A.KEY2) > 2                 
        )                                           
;                                                   
SELECT *               
  FROM SESSION.TABLE1   
;                       
SELECT *               
  FROM SESSION.TABLE2   
;


Contents of the first two selects of tables, before the updates

Table1
Code:

   +----------------------------------------+
   | KEY1 | KEY2 | FIELD1 | FIELD2 | FIELD3 |
   +----------------------------------------+
 1_| 101  | 54   | 1001   | BLABLA | BLABLA |
 2_| 102  | 54   | 1001   | BLABLA | BLABLA |
 3_| 103  | 54   | 1001   | BLABLA | BLABLA |
 4_| 104  | 54   | 1001   | BLABLA | BLABLA |
 5_| 105  | 55   | 1001   | BLABLA | BLABLA |
 6_| 104  | 50   | 1002   | BLABLA | BLABLA |
   +----------------------------------------+


Table2
Code:

    +-----------------------------------------+
    | KEY1 | FIELD1 | FIELD2 | FIELD3 |  TS   |
    +-----------------------------------------+
  1_| 101  | 1001   | A11    | A22    | T1    |
  2_| 101  | 1001   | B11    | B22    | T2    |
  3_| 102  | 1001   | C11    | C22    | T3    |
  4_| 102  | 1001   | D11    | D22    | T31   |
  5_| 103  | 1001   | E11    | E22    | T4    |
  6_| 104  | 1001   | F11    | F22    | T5    |
    +-----------------------------------------+


Contents of the second two selects of tables, after the update

Table1
Code:

   +----------------------------------------+
   | KEY1 | KEY2 | FIELD1 | FIELD2 | FIELD3 |
   +----------------------------------------+
 1_| 101  | 54   | 1001   | B11    | B22    |
 2_| 102  | 54   | 1001   | D11    | D22    |
 3_| 103  | 54   | 1001   | E11    | E22    |
 4_| 104  | 54   | 1001   | F11    | F22    |
 5_| 105  | 55   | 1001   | BLABLA | BLABLA |
 6_| 104  | 50   | 1002   | BLABLA | BLABLA |
   +----------------------------------------+


Table2
Code:

   +-----------------------------------------+
   | KEY1 | FIELD1 | FIELD2 | FIELD3 |  TS   |
   +-----------------------------------------+
 1_| 101  | 1001   | A11    | A22    | T1    |
 2_| 101  | 1001   | B11    | B22    | T2    |
 3_| 102  | 1001   | C11    | C22    | T3    |
 4_| 102  | 1001   | D11    | D22    | T31   |
 5_| 103  | 1001   | E11    | E22    | T4    |
 6_| 104  | 1001   | F11    | F22    | T5    |
   +-----------------------------------------+
                                             


Is this the result you were looking for? In the requirements at in your post you said ?more than 2 rows? which means 3 or more. If you wanted 2 or more change the ?> 2? to ?> 1?.


If you have any questions about the Update SQL, please do not hesitate to come back and ask.


Dave
Back to top
View user's profile Send private message
Click here, If this post answer your question. <-- Click on right mark icon. If this post answer your question.
bolastuff1975
Member


Joined: 21 Sep 2007
Posts: 8

Answer was approved by Question owner.
Useful Post
0 votes
Not an useful post
Greate Post!! Salute Author
0 salutes

PostPosted: Mon Sep 24, 2007 7:27 pm    Post subject: Reply with quote

Dave,
when i try it here, i get a message taht the inner query is listing more than 1 row.

I beleive 'where exists' condition checks if KEY2 is satisfying our update criteria and results more than 1 key1 but I need to pick single row(KEY1) for updates.

Thanks
Back to top
View user's profile Send private message
Click here, If this post answer your question. <-- Click on right mark icon. If this post answer your question.
DavidatK
Active Member


Joined: 27 Mar 2007
Posts: 65
Location: Troy, MI USA

Answer was approved by Question owner.
Useful Post
0 votes
Not an useful post
Greate Post!! Salute Author
0 salutes

PostPosted: Tue Sep 25, 2007 4:12 am    Post subject: Reply with quote

bolastuff1975,

So, your getting a -811.

The ?WHERE EXISTS? returns only a ?True|False? indicator and will not give you a -811,

Nor will the innermost SET ?SELECT MAX(TS)?. There can only be one MAX timestamp, even if multiple rows have this timestamp. Therefore it must be giving you the -811 in the SET outer ?SELECT? This will occur if you have multiple rows on Table2 with the same KEY1 and Timestamp.

What version of DB2 are you using ?
Back to top
View user's profile Send private message
Click here, If this post answer your question. <-- Click on right mark icon. If this post answer your question.
Veera
Moderator


Joined: 22 Feb 2006
Posts: 111

Answer was approved by Question owner.
Useful Post
0 votes
Not an useful post
Greate Post!! Salute Author
0 salutes

PostPosted: Tue Sep 25, 2007 4:51 am    Post subject: Reply with quote

bolastuff1975,

I agree with Dave..explanation for resulting in more than one row in the
co-realted sub-query.

Even the below query what you have given in your initial post.

update table1
set (b.field3,b.field4)
=(select field1,field2)
from table1 a,table-2 b
where a.key1 = b.key1
and timestamp = (select max(ts) from table2 c
where c.key1 = b.key1)
)

Assume the below data for 2 fields in question

KEY1 TS
100 100
100 100
100 99

In this scenario for max(TS) for a key1=100 the query will give the
first 2 rows , the only scenario under which you will get a unique row is
only when you have a distinct value for a TS field.

Now what is important is

1) Whether more than one rec can have the same TS in PRODUCTION
or live scenario...may be your test data has 2 rows with same TS.

You need to cross check it, I blive that if you have different timestamps
then it will result in only one row.

2) If yes you can have same TS for more than one row

Then its just a thought what you can possibly do ,if your
requirement allows is add equality of one more field
say FIELD-NEW which has a distinct values for a max(TS)


where a.key1 = b.key1
and timestamp = (select max(ts) from table2 c
where c.key1 = b.key1
and C.FIELD-NEW = B.FIELD-NEW)
)



3) If TS will not be same for more than one rec then your intial query
should be good.

Please let us know how you have finally handled it.

Thanks,
Veera.
Back to top
View user's profile Send private message
Click here, If this post answer your question. <-- Click on right mark icon. If this post answer your question.
bolastuff1975
Member


Joined: 21 Sep 2007
Posts: 8

Answer was approved by Question owner.
Useful Post
0 votes
Not an useful post
Greate Post!! Salute Author
0 salutes

PostPosted: Wed Sep 26, 2007 6:21 pm    Post subject: Reply with quote

Dave & Veera,

Thanks, very much for u r help. It was not a problem with u r query. it was due to the data in the table.

Cheers
Back to top
View user's profile Send private message
Click here, If this post answer your question. <-- Click on right mark icon. If this post answer your question.
bolastuff1975
Member


Joined: 21 Sep 2007
Posts: 8

Answer was approved by Question owner.
Useful Post
0 votes
Not an useful post
Greate Post!! Salute Author
0 salutes

PostPosted: Thu Sep 27, 2007 11:20 pm    Post subject: Reply with quote

Further, to the given query. i am intrested in findign the records that have timestamp < ts3.
Assuming t5>t4>t31>t3>t2>t1.
i odnt want to update 103, 104 of KEY1.
Back to top
View user's profile Send private message
Click here, If this post answer your question. <-- Click on right mark icon. If this post answer your question.
Display posts from previous:   
Post new topic   Reply to topic    mainframegurukul.com Forum Index -> DB2 SQL - DB2 PROGRAMMING All times are GMT + 5 Hours
Page 1 of 1



 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Related topics
 Topics   Replies   Author   Views   Last Post 
No new posts Need to put records a Sorting order as mentioned below. 13 Yash1911 4714 Wed Oct 31, 2012 8:18 pm
dbzTHEdinosauer View latest post
No new posts how to deal with the below scenario using iebgener 7 mainframes_6002 4758 Wed Dec 07, 2011 10:34 am
DikDude View latest post
No new posts Coulu someone explain the parameters in the below code? 1 lovely sweety 6411 Thu Jun 09, 2011 3:50 pm
dbzTHEdinosauer View latest post
No new posts Unable to achieve below scenario using ICETOOL 24 sirianangel 20958 Sat Oct 23, 2010 12:45 am
Frank Yaeger View latest post
No new posts Query For IEBPTPCH 2 padmajeet.gaikwad 8869 Mon Oct 26, 2009 10:25 am
padmajeet.gaikwad View latest post
 





Cobol Tutorial
This cobol tutorial covers most of the important topics like STRING, UNSTRING, COMP, COMP-3.....
DB2 Tutorial
DB2 Tutorial focuses on DB2 COBOL Programming. Explains in simple language. Some Chapters are locked, Forum members have free access to these chapters
CICS Tutorial
This CICS tutorial covers CICS concepts and CICS Basics, CICS COBOL Programming...
JCL Tutorial
This is most popular JCL tutorial from mainframegurukul. It does contain important jcl ....
SORT Tutorial
This Tutorial covers all important aspects of DFSORT. Has more SORT examples
INTERVIEW Questions
This page covers important interview questions
Mainframe Jobs
Mainframe Jobs posted by members of mainframegurukul forum
FaceBook Page
MainframeGurukul FaceBook Page
LinkedIn Page
MainframeGurkul Linkedin Page

This widget requires Flash Player 9 or better







Go to top of the page
 

Online ABEND Reference ||  JCL References ||  COBOL References ||  VSAM References ||  Tutorials by Drona Series ||  SQL tutorial ||  BOOKS  ||  DB2 INTERVIEW QUESTIONS ||  COBOL INTERVIEW QUESTIONS  ||  JCL INTERVIEW QUESTIONS ||  JCL2 INTERVIEW QUESTIONS ||  VSAM INTERVIEW QUESTIONS ||  CICS INTERVIEW QUESTIONS  ||  Online tutorials ||  Online ABEND Reference ||  JCL References ||  COBOL References ||  VSAM References ||  Tutorials by Drona Series ||  SQL tutorial ||  BOOKS  ||  SiteMap  ||  Expeditor Tutorial  ||  FILE-AID Tutorial  ||  Changeman Tutorial  ||  COBOL   ||  DB2   ||  JCL  ||  CICS  ||  VSAM  ||  DB2 Interview Questions ( 110 )   || Simple JCL Tutorials  || JCL Tutorial from MainframeGurukul.com   || Simple JCL Tutorial - Chapter1 ;|| Mainframe Forum - Tutorials  || Mainframe Tutorials || Mainframe CICS tutorial|| Mainframe COBOL Tutorial

Drona Educational Forums - Mainframe Cobol DB2 CICS Board
Powered by phpBB

mainframetopic-2664.html