MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help in writing Query

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
edkir98
Beginner


Joined: 27 Aug 2007
Posts: 102
Topics: 42
Location: Chennai

PostPosted: Mon Oct 06, 2008 4:12 am    Post subject: Need help in writing Query Reply with quote

This is my condition.
I got fx_table which maintains the details of all the foreign exchange rates. It has a column called as from_currency, to_currency, fx_rate and effective_timestamp. The to_currency is always USD. I will use a cursor to fetch today's transactions fom the transaction table tran_table and if the used currency of transaction(used_currency) is australian dollar then i need to multiply the transaction amount with the latest fx_rt..

something like this.

Code:
SELECT
   tran_number,
   used_currency,
   case used_currency
      when 'USD' then tran_amt
      else cast(tran_amt * (select fx_rate
               from fx_table
                   where to_currency = 'USD'
                and from_currency = used_currency
                and effective_timestamp =
                  (select max(effective_timestamp)
                     from fx_table
                    where to_currency = 'USD'
                      and from_currency = used_currency)
         )
      end as total_amt
FROM
   tran_table
   
   


Can someone help..
_________________
Thanks
Back to top
View user's profile Send private message Yahoo Messenger
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Mon Oct 06, 2008 11:06 am    Post subject: Reply with quote

Here is my guess.

Note:

1 Since to_currency is always USD, it is not checked
2 From you query it looked taht you want to do for all non USD curency, not only just AUD
3 It does not check if select is empty or not, you need to handle that
4 I am also learning DB2 so let me know what mistakes you find.

Code:
update tran_table tt
  set tran_amt = tt.tran_amt *
                   (select fx_rate from fx_table ft
                      where ft.from_currency = tt.used_currency
                        and ft.effective_timestamp =
                              (select max(effective_timestamp)
                                 from fx_table ft2
                                 where ft2.from_currency = tt.used_currency
                               )
                     )
  where tt.from_currency <> 'USD'
Back to top
View user's profile Send private message Send e-mail
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Mon Oct 06, 2008 1:04 pm    Post subject: Reply with quote

edkir98,

Untested sql

Code:

SELECT A.TRAN_NUMBER                                                   
      ,A.USED_CURRENCY                                                 
      ,CASE A.USED_CURRENCY                                           
            WHEN 'AUS' THEN A.TRAN_AMT * B.FX_RATE                     
            ELSE A.TRAN_AMT                                           
            END                                                       
  FROM tran_table A                                                     
      ,(SELECT FX_RATE                                                 
         FROM fx_table                                                 
        WHERE TO_CURRENCY   = 'USD'                                   
          AND FROM_CURRENCY = 'AUS'                                   
          AND EFFECTIVE_TIMESTAMP = (SELECT MAX(EFFECTIVE_TIMESTAMP)   
                                       FROM fx_table                 
                                      WHERE TO_CURRENCY   = 'USD'     
                                        AND FROM_CURRENCY = 'AUS'))B   
;


Dibakar,

Quote:

1 Since to_currency is always USD, it is not checked
2 From you query it looked taht you want to do for all non USD curency, not only just AUD
3 It does not check if select is empty or not, you need to handle that
4 I am also learning DB2 so let me know what mistakes you find.


1. The object is to get all the rows from the transaction table and only if the used currency is an aussie dollar it needs to be multiplied with exchange rate
2. You are trying to update the table where as OP wants a select
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Mon Oct 06, 2008 4:54 pm    Post subject: Reply with quote

Kolusu,

I wrongly thought that Ed is trying to update.

Thanks,
Diba.
Back to top
View user's profile Send private message Send e-mail
edkir98
Beginner


Joined: 27 Aug 2007
Posts: 102
Topics: 42
Location: Chennai

PostPosted: Mon Oct 06, 2008 11:21 pm    Post subject: Re Reply with quote

hi Kolusu and diba.
thanks for the help

Kolusu,
Quote:
The object is to get all the rows from the transaction table and only if the used currency is an aussie dollar it needs to be multiplied with exchange rate

i think i missed a point..the issue is used_currency may be anything, something out of all the currencies in the world. So the used_currency for a transaction has to be queried on to the fx_rate table as a record is fetched from the transaction table.

diba,
the query you had used would not work..because i cannot use an output variable from a select clause directly onto an operand for multiplication operator..even that was the reason my query dint work.. it would give an sqlerror.

i've found a solution bt am not sure if it can be written effectively..

Code:
   SELECT
      x.tran_number,
      x.used_currency,
      case x.used_currency
         when 'USD' then x.tran_amt
         else cast(x.tran_amt * y.fx_rate) as bigint
         end as total_amt
   FROM
      tran_table   X,
         (   SELECT  A.from_currency
                     ,A.fx_rate                       
                FROM  fx_rate A             
                  ,(SELECT B.from_currency                   
                          ,MAX(effective_timestamp)  AS effective_timestamp     
                      FROM  fx_rate B       
                     WHERE B.to_currency    = 'USD'           
                       AND B.from_currency <> 'USD'           
                     GROUP BY B.from_currency                 
                    )                           AS C       
                 WHERE A.to_currency    = 'USD'               
                   AND A.from_currency <> 'USD'               
                   AND A.from_currency  = c.from_currency       
                   AND A.effective_timestamp    = C.effective_timestamp      ) Y

_________________
Thanks
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database 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


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group