1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

How to union two tables, and make one "override" the other for similar records?

Discussion in 'SQL - Questions and Answers' started by Ben, Dec 27, 2006.

  1. Ben Guest

    Dialect: SQL 92
    I have a list of prices for a stocks on each day in a table. I want to make a table that can override the price for a stock on a particular day. If XYZ is $1 on 1/1/2007 according to my main price table, I might want to substitute in $2 for that day instead in my override table. Then, I'd like to union the two, with the $2 record showing as the price for that day.
  2. Ben Guest

    Dialect: SQL 92
    Found a solution:

    SQL:

    SELECT MktCode,Date,Price
    FROM tblStockPrices as t1
    LEFT JOIN tblOverridePrices as t2
    ON t1.date=t2.date and t1.mktcode=t2.mktcode
    WHERE
    T2.ID is null
    UNION
    SELECT MktCode,Date,Price
    FROM tblOverridePrices
  3. Dimitar Guest

    Dialect: SQL 92
    SQL:

    select
    distinct t1.mktcode, t1.date, case when t2.mktcode is null then t1.price else t2.price end
    from
    tblstockprices t1
    left join tbloverrideprices t2 on t1.mktcode=t2.mktcode and t1.date=t2.date

Share This Page