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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:17:51+00:00 2026-05-26T00:17:51+00:00

Lets imagine you have a portfolio to value containing 4 assets that needs to

  • 0

Lets imagine you have a portfolio to value containing 4 assets that needs to be priced. The SourceID dictates the priority that the PriceSource should be given and the lowest SourceID should be retrieved. Where no price is available from the EODPrice table, then the AverageBookCost should be retrieved (which resides in a different Table). I am using SQL Server 2005.

As an example, lets imagine that EODPrice table has the following data (‘TEST004’ is missing):

SourceID    Date                   Ticker     Price
0           2011-08-02 00:00:00    TEST001    104.50
1           2011-08-01 00:00:00    TEST001    100.00
1           2011-08-02 00:00:00    TEST001    105.00
1           2011-08-04 00:00:00    TEST001    115.00
2           2011-08-03 00:00:00    TEST001    109.38
2           2011-08-04 00:00:00    TEST001    114.24
1           2011-08-01 00:00:00    TEST002    9.99
1           2011-08-02 00:00:00    TEST002    9.89
1           2011-08-03 00:00:00    TEST002    9.79
1           2011-08-04 00:00:00    TEST002    9.69
0           2011-08-03 00:00:00    TEST003    0.42
2           2011-08-01 00:00:00    TEST003    0.33
2           2011-08-02 00:00:00    TEST003    0.38
2           2011-08-03 00:00:00    TEST003    0.28
2           2011-08-04 00:00:00    TEST003    0.45

Lets imagine that we would like to build a Select statement where we retrieve the EODPrice for the following assets (‘TEST001′,’TEST002’, ‘TEST003’, ‘TEST004’). Note that ‘TEST004’ is a new asset that has just hit the Market and no price is available yet in the EODPrice table or the Market.

Further, lets imagine that ALL Tickers for any Date has a non-NULL AverageBookCost field from a RunningTotal Table. (i.e. SELECT AverageBookCost FROM RunningTotal WHERE Ticker = ‘TEST004’ AND Date = ‘2011-08-03’ would return the value 0.15 say).

How do I build the most efficient ‘Correlated’ or ‘COALESCE / ISNULL’ Query:

SELECT Ticker, SourceID, Price
   ???
   WHERE [Date] = '2011-08-03' 
   AND [Ticker] IN ('TEST001','TEST002', 'TEST003' and 'TEST004') 

That would return the following Table: (Note as ‘TEST004’ Price is the AverageBookCost and not in the EODPrice table, then the SourceID is set to NULL to indicate that the Price is from the RunningTotal table:

Ticker   SourceID   Price
TEST1    2          109.38
TEST2    1          9.79
TEST3    0          0.42
TEST4    NULL       0.15

Many thanks,
Bertie.

  • 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-26T00:17:52+00:00Added an answer on May 26, 2026 at 12:17 am

    I most likely overcomplicated things but this might get you started.

    In a nutshell, the reasoning goes like this

    • In a subselect, SELECT all tickers for given date with their lowest SourceID.
    • JOIN the results of this subselect back with the original table. This step allows the price to be retrieved for each ticker with given date and lowest SourceID.
    • FULL OUTER JOIN the previous result with the average book cost. This step adds to each row the average price for that ticker and adds rows for tickers that don’t have a record returned from Pricetable.
    • SELECT from these results the Price if available, otherwise select from the appended columns the average book cost.

    SQL Statement

    SELECT  [Ticker] = ISNULL(pt.Ticker, pt_avg.Ticker)
            , [SourceID] = pt.SourceID
            , [Price] = ISNULL(pt.Price, pt_avg.AverageBookCost)
    FROM    EODPriceTable pt 
            INNER JOIN (
              SELECT  SourceID = MIN(SourceID), Ticker, Date
              FROM    EODPriceTable
              WHERE   Date = '2011-08-03 00:00:00'
              GROUP BY
                      Ticker, Date
             ) pt_min ON  pt_min.SourceID = pt.SourceID 
                          AND pt_min.Ticker = pt.Ticker
                          AND pt_min.Date = pt.Date
             FULL OUTER JOIN (
                SELECT  Ticker, AverageBookCost
                FROM    RunningTable
             ) pt_avg ON pt_avg.Ticker = pt.Ticker
    WHERE  ISNULL(pt.Ticker, pt_avg.Ticker) IN ('TEST001', 'TEST002', 'TEST003', 'TEST004')
    

    Test script

    ;WITH EODPriceTable (SourceID, Date, Ticker, Price) AS (
      SELECT 0, '2011-08-02 00:00:00', 'TEST001', 104.50
      UNION ALL SELECT 1, '2011-08-01 00:00:00', 'TEST001', 100.00
      UNION ALL SELECT 1, '2011-08-02 00:00:00', 'TEST001', 105.00
      UNION ALL SELECT 1, '2011-08-04 00:00:00', 'TEST001', 115.00
      UNION ALL SELECT 2, '2011-08-03 00:00:00', 'TEST001', 109.38
      UNION ALL SELECT 2, '2011-08-04 00:00:00', 'TEST001', 114.24
      UNION ALL SELECT 1, '2011-08-01 00:00:00', 'TEST002', 9.99
      UNION ALL SELECT 1, '2011-08-02 00:00:00', 'TEST002', 9.89
      UNION ALL SELECT 1, '2011-08-03 00:00:00', 'TEST002', 9.79
      UNION ALL SELECT 1, '2011-08-04 00:00:00', 'TEST002', 9.69
      UNION ALL SELECT 0, '2011-08-03 00:00:00', 'TEST003', 0.42
      UNION ALL SELECT 2, '2011-08-01 00:00:00', 'TEST003', 0.33
      UNION ALL SELECT 2, '2011-08-02 00:00:00', 'TEST003', 0.38
      UNION ALL SELECT 2, '2011-08-03 00:00:00', 'TEST003', 0.28
      UNION ALL SELECT 2, '2011-08-04 00:00:00', 'TEST003', 0.45
    )
    , RunningTable (Ticker, AverageBookCost) AS (
      SELECT 'TEST004', 0.15
      UNION ALL SELECT 'TEST003', 0.09
    )
    SELECT  [Ticker] = ISNULL(pt.Ticker, pt_avg.Ticker)
            , [SourceID] = pt.SourceID
            , [Price] = ISNULL(pt.Price, pt_avg.AverageBookCost)
    FROM    EODPriceTable pt 
            INNER JOIN (
              SELECT  SourceID = MIN(SourceID), Ticker, Date
              FROM    EODPriceTable
              WHERE   Date = '2011-08-03 00:00:00'
              GROUP BY
                      Ticker, Date
             ) pt_min ON  pt_min.SourceID = pt.SourceID 
                          AND pt_min.Ticker = pt.Ticker
                          AND pt_min.Date = pt.Date
             FULL OUTER JOIN (
                SELECT  Ticker, AverageBookCost
                FROM    RunningTable
             ) pt_avg ON pt_avg.Ticker = pt.Ticker
    WHERE  ISNULL(pt.Ticker, pt_avg.Ticker) IN ('TEST001', 'TEST002', 'TEST003', 'TEST004')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ok im a newbie on sessions lets imagine that we have a little login
Lets imagine that I have videos and each video can have few tags (maximal
Lets imagine my situation (it's fake, of course)... I have web-site that have 1000
imagine that i have a property called NextSend representing DateTime Value 4/11/2011 10:30:00 AM
Let's imagine that I have list of files at host1 find /path/to -name *.jpg
Let's imagine that I have a result of evaluating XPath expression //node/@*. MSXML6 returns
Lets suppose that I have the following simple query var q = from p
Lets say I have a WPF application that shows a ListBox with an ArrayList
Lets imagine, i have a .dll / Solution in MS Visual Studio 2010 with
We have a web-application that lets the users trigger a request to an external

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.