IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

DB2 - HOW to USE CASE in WHERE clause


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Amit_Singh47

New User


Joined: 15 May 2013
Posts: 22
Location: India

PostPosted: Mon May 20, 2013 9:30 am
Reply with quote

Apologies for not specifying that I needed this in the same SELECT and not 3 diff SELECTS in SAME QUERY.
Back to top
View user's profile Send private message
Pandora-Box

Global Moderator


Joined: 07 Sep 2006
Posts: 1592
Location: Andromeda Galaxy

PostPosted: Mon May 20, 2013 10:17 am
Reply with quote

Amit,

If you are not sure about performance you need to execute EXPLAIN against the query

Also please read manuals on how to decrypt the message from EXPLAIN
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Mon May 20, 2013 10:51 am
Reply with quote

Hi Amit,

Amit_Singh47 wrote:
these approaches suggested may work for unique combination of two fields, like the one i said

Student_id
SSN

what if the realationship between student_id and ssn is m:n.

i.e. more than one student can have same SSN and Student_id.
just in case)it will never happen)I just want to know if this scenario also can be handled using ONLY one query.


SSN1 sid1
SSN2 sid2
SSN3 sid3
SSN1 sid4
SSN1 sid5
SSN4 sid1
SSN5 sid2


Can you please explain what kind of output you are expecting for above set of data and what will be your input host variables?

Regards,
Chandan
Back to top
View user's profile Send private message
Amit_Singh47

New User


Joined: 15 May 2013
Posts: 22
Location: India

PostPosted: Mon May 20, 2013 2:38 pm
Reply with quote

okay...

if user enters only SSN say SSN1 then output SSN1 sid1
SSN1 sid4
SSN1 sid5

if user enters student ID say sid2SSN2 sid2
SSN5 sid2

if user enters both student ID (sid1) and SSN(SSN1)
SSN1 sid1

INPUT HOST VARIABLES:
1) WS-SSN; if in the CICS map SSN field is not inputed this is defaulted to spaces
2) WS-SID;if in the CICS map SID field is not inputed this is defaulted to spaces
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Mon May 20, 2013 3:32 pm
Reply with quote

Try this it's untested

Code:
WHERE( STUDENT_ID  = :WS-STUDENT-ID
AND  STUDENT_SSN = CASE WHEN :WS-SSN >SPACES
THEN :WS-SSN ELSE STUDENT_SSN END)                     
 OR (STUDENT_SSN = :WS-SSN
          AND STUDENT_ID = CASE WHEN :WS-STUDENT-ID > SPACES
          THEN :WS-STUDENT-ID ELSE STUDENT_ID END )

Here before execution of query if WS-SSN or WS-STUDENT-ID then populate them with low values so that they will not be true for gretaer than spaces

See if it helps

Regards,
Chandan
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19243
Location: Inside the Matrix

PostPosted: Mon May 20, 2013 7:28 pm
Reply with quote

Hello,

Quote:
I would have ~ billion records in database. Would using a UNION/UNION All be a good option?
Processing a billion records will take much more time than you desire if the querie(s) cannot be handled by key reads and even these may bog down (depending on how many "duplicate entries" there might be).

How are there a billion student records?

If we knew more about the process, we might be able to offer better suggestions.
Back to top
View user's profile Send private message
seagull

New User


Joined: 28 May 2007
Posts: 24
Location: Dublin

PostPosted: Tue May 21, 2013 1:40 am
Reply with quote

In an ideal world, the database should be set up so that those fields are either required or hold nulls, rather than having spaces.
Back to top
View user's profile Send private message
chandan.inst

Active User


Joined: 03 Nov 2005
Posts: 275
Location: Mumbai

PostPosted: Tue May 21, 2013 12:20 pm
Reply with quote

chandan.inst wrote:
Code:
WHERE( STUDENT_ID  = :WS-STUDENT-ID
AND  STUDENT_SSN = CASE WHEN :WS-SSN >SPACES
THEN :WS-SSN ELSE STUDENT_SSN END)                     
 OR (STUDENT_SSN = :WS-SSN
          AND STUDENT_ID = CASE WHEN :WS-STUDENT-ID > SPACES
          THEN :WS-STUDENT-ID ELSE STUDENT_ID END )

Here before execution of query if WS-SSN or WS-STUDENT-ID then populate them with low values so that they will not be true for gretaer than spaces


I have tested above query in IBM data studio and its working as expected.
The only thing is somehow > SAPCES check not working in Data studio. You may check that with cobol code so I modified the query as below

Code:
WHERE( STUDENT_ID  = :WS-STUDENT-ID
AND  STUDENT_SSN = CASE WHEN :WS-SSN-PRESENT = 'Y'
THEN :WS-SSN ELSE STUDENT_SSN END)                     
 OR (STUDENT_SSN = :WS-SSN
          AND STUDENT_ID = CASE WHEN :WS-STUDENT-ID-PRESENT = 'Y'
          THEN :WS-STUDENT-ID ELSE STUDENT_ID END )


Here WS-SSN-PRESENT and WS-STUDENT-ID-PRESENT needs to be populated with 'Y' before query execution as per the input data.

Agree with Dick if you need to suggestion with performance we will need more inputs or you can also get in touch with DBA's at your shop if required

Let us know if it works for you

Regards,
Chandan
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2 Goto page Previous  1, 2

 


Similar Topics
Topic Forum Replies
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts To search DB2 table based on Conditio... DB2 1
No new posts Zunit Test case editor error Testing & Performance 4
No new posts NOT IN clause in COBOL pgm COBOL Programming 8
No new posts SUSBSCRIPT WITH SIGN IN PIC CLAUSE COBOL Programming 3
Search our Forums:

Back to Top