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 > Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-17-08, 15:26
Natasha82 Natasha82 is offline
Registered User
 
Join Date: Apr 2008
Posts: 8
Query

I have a query as below;

Select TRN-AMT
Into WK-TRN-AMT
From TRNTABLE
Where TRN-DATE <= WK-TRN-DATE.

In TRNTABLE the TRN-AMT can hold both Positive (>= 0) and
Negative (< 0) values.

Is there anyway I can adjust the above query to get TRN-AMT to
be moved INTO WK-POS-AMT when TRN-AMT >= 0 and to WK-NEG-AMT
in all other situations.

Any help will be much appreciated.

Thanks.

Natasha.
Reply With Quote
  #2 (permalink)  
Old 04-17-08, 15:35
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
You would have to make it two different queries:

Select TRN-AMT
Into WK-POS-AMT
From TRNTABLE
Where TRN-DATE <= WK-TRN-DATE
and trn-amt >= 0.

Select TRN-AMT
Into WK-NEG-AMT
From TRNTABLE
Where TRN-DATE <= WK-TRN-DATE
and trn-amt < 0.


Andy
Reply With Quote
  #3 (permalink)  
Old 04-17-08, 15:43
Natasha82 Natasha82 is offline
Registered User
 
Join Date: Apr 2008
Posts: 8
Same Query

Sorry, I'm looking for a single query... Is it possible ? - N.


Quote:
Originally Posted by ARWinner
You would have to make it two different queries:

Select TRN-AMT
Into WK-POS-AMT
From TRNTABLE
Where TRN-DATE <= WK-TRN-DATE
and trn-amt >= 0.

Select TRN-AMT
Into WK-NEG-AMT
From TRNTABLE
Where TRN-DATE <= WK-TRN-DATE
and trn-amt < 0.


Andy
Reply With Quote
  #4 (permalink)  
Old 04-17-08, 15:44
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
No, it is not possible with a singe query.

Andy
Reply With Quote
  #5 (permalink)  
Old 04-17-08, 15:49
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Well, I guess you could do it this way:
Code:
Select 
  case when TRN-AMT >= 0 then TRN-AMT else null end, 
  case when TRN-AMT < 0 then TRN-AMT else null end
Into WK-POS-AMT, WK-NEG-AMT
From TRNTABLE
Where TRN-DATE <= WK-TRN-DATE
I did not test it so you may need to cast nulls to the TRN-AMT data type before it works.
Reply With Quote
  #6 (permalink)  
Old 04-17-08, 16:00
Cougar8000 Cougar8000 is offline
Registered User
 
Join Date: Nov 2005
Location: IL
Posts: 554
You might try this, but depending on your cituation or how you are coding it after words it might not work

Code:
Select 
CASE WK-POS-AMT WHEN trn-amt >= 0 then TRN-AMT else NULL END,
CASE WK-NEG-AMT WHEN trn-amt < 0 then TRN-AMT else NULL END
Into WK-POS-AMT, WK-NEGAMT
From TRNTABLE
Where TRN-DATE <= WK-TRN-DATE
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows

DB2 v9.1.0.2 os 5.3.0.0
Reply With Quote
  #7 (permalink)  
Old 04-17-08, 16:01
Cougar8000 Cougar8000 is offline
Registered User
 
Join Date: Nov 2005
Location: IL
Posts: 554
Oups. n_i we must be on the same page now

natasha, try substituting 0(zero) instead of NULL come to think of it. Then you can do SUM function to get your summary data.
__________________
--
IBM Certified DBA on DB2 for Linux, UNIX, and Windows

DB2 v9.1.0.2 os 5.3.0.0
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