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 8637553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:30:21+00:00 2026-06-12T10:30:21+00:00

I have 2 tables, one which holds information about contracts and another which holds

  • 0

I have 2 tables, one which holds information about contracts and another which holds exchange rates.

The exchange rate table is made up of 3 fields
– two primary keys, one for the currency code (e.g. EURO) and one for the start date of the exchange rate

-One field which holds the exchange rate

The contract table just holds standard information about a contract along with the currency code as a foreign key to the exchange rate table and the date of the contract.

I’d like to be able to select each contract individually along with the applicable exchange rate for the date of the contract. So, for example, if my contract date is on the 20th of September 2012 and I have three exchange rates for the currency of that contract, one which starts on the 1st of September, one which starts on the 12th of September, and one that starts on the 30th September. How would I make sure that only the exchange rate for the 12th of September is returned along with the contract data and not any other rates, as the 20th of September falls within that ‘period’.

Would some sort of subquery be necessary?

I am using SQL Server 2005.

  • 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-06-12T10:30:23+00:00Added an answer on June 12, 2026 at 10:30 am

    The problem with the data structure is that you have a first date but not a last date for the rates. One way to do this is with a correlated subquery. However, I prefer using the ranking function to assign the next date:

    with fxr as (
         select fx.*,
                row_number() over (partition by ccy order by exdate) as seqnum
         from fx
        ),
         fxrates as (
         select fxr.*, coalesce(fxnext.exdate, getdate()+1) as next_exdate
         from fxr left outer join
              fxr fxnext
              on fxr.ccy = fxnext.ccy and
                 fxr.seqnum + 1 = fxnext.seqnum
        )
    select c.*, f.fxrate
    from contracts c left outer join
         fxrates f
         on c.ccy = f.ccy and
            c.thedate >= f.exdate and
            c.thedate < f.next_exdate
    

    This is assigning the end date for the currency period in the fxrates CTE. For the most recent, the end date is tomorrow — presumably, all fxrates are in the past. It then joins to this table to get the appropriate rate for the contract.

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

Sidebar

Related Questions

I have 4 tables which holds information about our Licenses. We have our own
I have two tables. One user table and one table in which results are
I have SMO code which copies tables from one database to another. It runs
I have a table which needs to link one of three seperate tables, but
I have a table which has several one to many relationships with other tables.
I have a database Student which contains about 20 tables. one of the tables
I have one table which contains events and dates, and another which contains the
I have two tables. One which holds the user status content and the other
In a school system I have 2 tables, one called Staff which holds records
I have a table that holds information about cities in a game, you can

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.