Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 640117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:55:04+00:00 2026-05-13T20:55:04+00:00

Consider two tables: Transactions , with amounts in a foreign currency: Date Amount =========

  • 0

Consider two tables:

Transactions, with amounts in a foreign currency:

     Date  Amount
========= =======
 1/2/2009    1500
 2/4/2009    2300
3/15/2009     300
4/17/2009    2200
etc.

ExchangeRates, with the value of the primary currency (let’s say dollars) in the foreign currency:

     Date    Rate
========= =======
 2/1/2009    40.1
 3/1/2009    41.0
 4/1/2009    38.5
 5/1/2009    42.7
etc.

Exchange rates can be entered for arbitrary dates – the user could enter them on a daily basis, weekly basis, monthly basis, or at irregular intervals.

In order to translate the foreign amounts to dollars, I need to respect these rules:

A. If possible, use the most recent previous rate; so the transaction on 2/4/2009 uses the rate for 2/1/2009, and the transaction on 3/15/2009 uses the rate for 3/1/2009.

B. If there isn’t a rate defined for a previous date, use the earliest rate available. So the transaction on 1/2/2009 uses the rate for 2/1/2009, since there isn’t an earlier rate defined.

This works…

Select 
    t.Date, 
    t.Amount,
    ConvertedAmount=(   
        Select Top 1 
            t.Amount/ex.Rate
        From ExchangeRates ex
        Where t.Date > ex.Date
        Order by ex.Date desc
    )
From Transactions t

… but (1) it seems like a join would be more efficient & elegant, and (2) it doesn’t deal with Rule B above.

Is there an alternative to using the subquery to find the appropriate rate? And is there an elegant way to handle Rule B, without tying myself in knots?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:55:04+00:00Added an answer on May 13, 2026 at 8:55 pm

    You could first do a self-join on the exchange rates which are ordered by date so that you have the start and the end date of each exchange rate, without any overlap or gap in the dates (maybe add that as view to your database – in my case I’m just using a common table expression).

    Now joining those “prepared” rates with the transactions is simple and efficient.

    Something like:

    WITH IndexedExchangeRates AS (           
                SELECT  Row_Number() OVER (ORDER BY Date) ix,
                        Date,
                        Rate 
                FROM    ExchangeRates 
            ),
            RangedExchangeRates AS (             
                SELECT  CASE WHEN IER.ix=1 THEN CAST('1753-01-01' AS datetime) 
                        ELSE IER.Date 
                        END DateFrom,
                        COALESCE(IER2.Date, GETDATE()) DateTo,
                        IER.Rate 
                FROM    IndexedExchangeRates IER 
                LEFT JOIN IndexedExchangeRates IER2 
                ON IER.ix = IER2.ix-1 
            )
    SELECT  T.Date,
            T.Amount,
            RER.Rate,
            T.Amount/RER.Rate ConvertedAmount 
    FROM    Transactions T 
    LEFT JOIN RangedExchangeRates RER 
    ON (T.Date > RER.DateFrom) AND (T.Date <= RER.DateTo)
    

    Notes:

    • You could replace GETDATE() with a date in the far future, I’m assuming here that no rates for the future are known.

    • Rule (B) is implemented by setting the date of the first known exchange rate to the minimal date supported by the SQL Server datetime, which should (by definition if it is the type you’re using for the Date column) be the smallest value possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do splitting fields into multiple tables ever yield faster queries? Consider the following two
Consider I have two tables, t1 and t2 t1 = (id, name), t2 =
Consider the following two (hypothetical) tables Temperature * day * time * lake_name *
Consider these two tables: --Subscriber_File--- ID GenreId FileName 01 1,2 TestFile.pdf --MasterGenre-- ID Genrename
Consider these two tables: CREATE TABLE GAME_ROUNDS( RoundId int primary key NOT NULL, GameId
Consider the following two MySQL tables: companies ----------------------- id ticker 1 AA 2 AAPL
Consider these two tables stored in a MySQL database. The first stores a list
Consider two tables transaction and category each having their own ID and information. A
Consider two tables as mentioned below - Table1 (id_col, name_col) Table2 (uid_col, code_col_1, code_col_2)
Let's consider two tables: First: Id Data 1 asd 2 buu And Second: UPD:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.