SQL Recipes (Beta II)
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread
  #1 (permalink)  
Old 12-27-2006, 10:01 PM
Ben
 
Posts: n/a
Default SQL 92 dialect question:

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


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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 01-04-2007, 12:22 AM
Ben
 
Posts: n/a
Default SQL 92 answer. Re: How to union two tables, and make one "override" the other for similar records?

Found a solution:

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
  #3 (permalink)  
Old 01-04-2007, 04:44 AM
Dimitar
 
Posts: n/a
Default SQL 92 answer. Re: How to union two tables, and make one "override" the other for similar records?

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
 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Edit/Delete Message Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Moderation Tools:



All times are GMT. The time now is 09:19 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2007 SQL Recipes

1 2 3 4 5 6 7