If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Select query should return different row

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-12-11, 07:27
Billa007 Billa007 is offline
Registered User
 
Join Date: Sep 2011
Posts: 107
Select query should return different row

Hi;

Below simple select query should return two different result set based on
the condition
Code:
SELECT t1.p_loc
     , t1.p_nor
     ,p_det 

      from table1 t1
Table1

Code:
p_loc   p_NOR        p_det
A11     1234         aaa
B11     1311         WWW
DEF     1111         DDD
DEF     2222         EEE
We will pass one flag variable(:ws-chk) value to the query from screen whether it will be 'P' or 'T'

Condition is

1. if the :ws-chk = 'P' means the select should return the P_LOC <> 'DEF' rows

2. if the :ws-chk = 'T' means the select should return the P_LOC = 'DEF' rows

Expected output

Based on condition 1
Code:
p_loc   p_NOR        p_det
A11     1234         aaa
B11     1311         WWW
Based on condition 2
Code:
p_loc   p_NOR        p_det
DEF     1111         DDD
DEF     2222         EEE
Please help
Reply With Quote
  #2 (permalink)  
Old 12-12-11, 07:41
przytula_guy przytula_guy is offline
Registered User
 
Join Date: Apr 2006
Location: Belgium
Posts: 1,159
sorry - wrong entry
__________________
Best Regards, Guy Przytula
Database Software Consultant
DB2 UDB LUW Certified V7-V8-V9-V9.7 DB Admin - Dprop..
Information Server Datastage Certified
http://www.infocura.be

Last edited by przytula_guy; 12-12-11 at 07:42. Reason: error
Reply With Quote
  #3 (permalink)  
Old 12-12-11, 21:34
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Quote:
Condition is

1. if the :ws-chk = 'P' means the select should return the P_LOC <> 'DEF' rows

2. if the :ws-chk = 'T' means the select should return the P_LOC = 'DEF' rows
Generally speaking, a condition
IF condition-1 THEN use condition-2 ELSE IF condition-3 THEN use condition-4
can be implemented in SQL
condition-1 AND condition-2 OR NOT condition-1 AND condition-3 AND condition-4

In your condition, condition-1 and condition-3 are mutually exclusive,
then "NOT condition-1 AND" can be removed.

As a cosequence,
your condition can be implemented in SQL like...
condition-1 AND condition-2 OR condition-3 AND condition-4
where
condition-1 is :ws-chk = 'P'
condition-2 is P_LOC <> 'DEF'
condition-3 is :ws-chk = 'T'
condition-4 is P_LOC = 'DEF'
Reply With Quote
  #4 (permalink)  
Old 12-13-11, 07:24
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
For clarity, I recommend to add some parenthesis. Although AND binds stronger than OR, it is obvious to the casual reader to have:
Code:
( condition-1 AND condition-2 ) OR ( condition-3 AND condition-4 )
It's just about maintainability...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 12-15-11, 11:25
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
Stolze,

I agree with you about OR.

I feel parentheses(for readability) are not so neccesary for AND or NOT.

For example:
order of readable and easy to understand representations may be (a) >= (b) >= (c) > (d) > (e) > (f).

(a) parenthesize conditions connected by OR and conditions prefiexed by NOT
Code:
       ( condition-1 AND ( NOT condition-2 ) AND condition-3 )
   OR  ( condition-4 )
   OR  ( ( NOT ( condition-5 AND condition-6 ) ) AND condition-7 )
/* is equivalent to */
       ( condition-1 AND ( NOT condition-2 ) AND condition-3 )
   OR  ( condition-4 )
   OR  ( ( ( NOT condition-5 ) OR ( NOT condition-6 ) ) AND condition-7 )
or
Code:
 ( c-1 AND ( NOT c-2 ) AND c-3 ) OR ( c-4 ) OR ( ( NOT ( c-5 AND c-6 ) ) AND c-7 )
(b) parenthesize conditions connected by OR
Code:
       ( condition-1 AND NOT condition-2 AND condition-3 )
   OR  ( condition-4 )
   OR  ( NOT ( condition-5 AND condition-6 ) AND condition-7 )
/* is equivalent to */
       ( condition-1 AND NOT condition-2 AND condition-3 )
   OR  ( condition-4 )
   OR  ( ( NOT condition-5 OR NOT condition-6 ) AND condition-7 )
(c) parenthesize compound conditions connected by OR
Code:
       ( condition-1 AND NOT condition-2 AND condition-3 )
   OR  condition-4
   OR  ( NOT ( condition-5 AND condition-6 ) AND condition-7 )
/* is equivalent to */
       ( condition-1 AND NOT condition-2 AND condition-3 )
   OR  condition-4
   OR  ( ( NOT condition-5 OR NOT condition-6 ) AND condition-7 )
(d) parenthesize conditions of right to OR
Code:
       condition-1 AND NOT condition-2 AND condition-3
   OR  ( condition-4 )
   OR  ( NOT ( condition-5 AND condition-6 ) AND condition-7 )
/* is equivalent to */
       condition-1 AND NOT condition-2 AND condition-3
   OR  ( condition-4 )
   OR  ( ( NOT condition-5 OR NOT condition-6 ) AND condition-7 )
(e) parenthesize compound conditions of right to OR
Code:
       condition-1 AND NOT condition-2 AND condition-3
   OR  condition-4
   OR  ( NOT ( condition-5 AND condition-6 ) AND condition-7 )
/* is equivalent to */
       condition-1 AND NOT condition-2 AND condition-3
   OR  condition-4
   OR  ( ( NOT condition-5 OR NOT condition-6 ) AND condition-7 )
(f) only neccesary parentheses
Code:
      condition-1 AND NOT condition-2 AND condition-3
  OR  condition-4
  OR  NOT ( condition-5 AND condition-6 ) AND condition-7
/* is equivalent to */
      condition-1 AND NOT condition-2 AND condition-3
  OR  condition-4
  OR  ( NOT condition-5 OR NOT condition-6 ) AND condition-7
or
Code:
 c-1 AND NOT c-2 AND c-3 OR c-4 OR NOT ( c-5 AND c-6 ) AND c-7

While I prefer (c) or (e) and sometimes use (b),
some person may prefer (a) or (b).


Stolze,

do you like (b) or (c)?

Last edited by tonkuma; 12-18-11 at 07:43. Reason: Add " and sometimes use (b)"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On