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

  • Home
  • SEARCH
  • 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 5966701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:50:15+00:00 2026-05-22T19:50:15+00:00

Suppose I have a table called Transaction and another table called Price. Price holds

  • 0

Suppose I have a table called Transaction and another table called Price. Price holds the prices for given funds at different dates. Each fund will have prices added at various dates, but they won’t have prices at all possible dates. So for fund XYZ I may have prices for the 1 May, 7 May and 13 May and fund ABC may have prices at 3 May, 9 May and 11 May.

So now I’m looking at the price that was prevailing for a fund at the date of a transaction. The transaction was for fund XYZ on 10 May. What I want, is the latest known price on that day, which will be the price for 7 May.

Here’s the code:

select d.TransactionID, d.FundCode, d.TransactionDate, v.OfferPrice
from Transaction d
    inner join Price v
        on v.FundCode = d.FundCode
        and v.PriceDate = (
            select max(PriceDate)
            from Price
            where FundCode = v.FundCode
            /* */ and PriceDate < d.TransactionDate 
        )

It works, but it is very slow (several minutes in real world use). If I remove the line with the leading comment, the query is very quick (2 seconds or so) but it then uses the latest price per fund, which is wrong.

The bad part is that the price table is minuscule compared to some of the other tables we use, and it isn’t clear to me why it is so slow. I suspect the offending line forces SQL Server to process a Cartesian product, but I don’t know how to avoid it.

I keep hoping to find a more efficient way to do this, but it has so far escaped me. Any ideas?

  • 1 1 Answer
  • 1 View
  • 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-22T19:50:16+00:00Added an answer on May 22, 2026 at 7:50 pm

    You don’t specify the version of SQL Server you’re using, but if you are using a version with support for ranking functions and CTE queries I think you’ll find this quite a bit more performant than using a correlated subquery within your join statement.

    It should be very similar in performance to Andriy’s queries. Depending on the exact index topography of your tables, one approach might be slightly faster than another.

    I tend to like CTE-based approaches because the resulting code is quite a bit more readable (in my opinion). Hope this helps!

    ;WITH set_gen (TransactionID, OfferPrice, Match_val)
    AS
    (
        SELECT d.TransactionID, v.OfferPrice, ROW_NUMBER() OVER(PARTITION BY d.TransactionID ORDER BY v.PriceDate ASC) AS Match_val
        FROM Transaction d
            INNER JOIN Price v
                ON v.FundCode = d.FundCode
        WHERE v.PriceDate <= d.TransactionDate
    )
    SELECT sg.TransactionID, d.FundCode, d.TransactionDate, sg.OfferPrice
    FROM Transaction d
        INNER JOIN set_gen sg ON d.TransactionID = sg.TransactionID
    WHERE sg.Match_val = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following data model: Suppose I have a table called SuperAwesomeData where each
Suppose I have a SQL table called AT_Devices with each record representing a piece
Suppose you have a table in SQL: Prices ------ 13.99 14.00 52.00 52.00 52.00
Suppose I have a table called 'test', into which there are three columns named
Suppose I have a MySQL table called MyTable, that looks like this: +----+------+-------+ |
Suppose I have a massive table called inactiveUsers and a search form. I want
Suppose I have a table called device as below: device_id(field) 123asf15fas 456g4fd45ww 7861fassd45 I
Suppose I have a table called Projects with a column called Budget with a
Let's suppose I have a table called 'user_products' and a corresponding model called UserProduct
Let's suppose I have a table called 'user_products' and a corresponding model called UserProduct

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.