472,780 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

Inserting unique data from one table into another

119 100+
Hi,

I need to insert asset pricing data from one table that is updated multiple times daily, to another table containing historical data. The fields in both tables are:

Symbol, Date, Price.

However, I would like to avoid duplicate Symbol/Date combinations (i.e. in the historical pricing table only one price exists for a symbol on a particular date), ideally overwriting earlier pricing on the same day.

I think I need to use the INSERT command but with some additional 'overwrite if Symbol/Date already present' command. This is what I currently have, which as you can see, only inserts the data.

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO AssetPrices
  2. SELECT *
  3. FROM LivePricing;
Any help would be greatly appreciated.
Feb 20 '07 #1
7 12345
ADezii
8,834 Expert 8TB
Hi,

I need to insert asset pricing data from one table that is updated multiple times daily, to another table containing historical data. The fields in both tables are:

Symbol, Date, Price.

However, I would like to avoid duplicate Symbol/Date combinations (i.e. in the historical pricing table only one price exists for a symbol on a particular date), ideally overwriting earlier pricing on the same day.

I think I need to use the INSERT command but with some additional 'overwrite if Symbol/Date already present' command. This is what I currently have, which as you can see, only inserts the data.

INSERT INTO AssetPrices
SELECT *
FROM LivePricing;

Any help would be greatly appreciated.
On Table AssestPrices set a 2 Field Composite Primary Key on [Symbol] and
[DATE]. This will ensure a unique combination of values in these 2 Fields.
Place this code wherever appropriate:

Expand|Select|Wrap|Line Numbers
  1. Dim MySQL As String
  2.  
  3. 'Avoid the dreaded About to Append and Can't Append prompts. Because of
  4. 'the Composite Primary Key, only uniques values based on [Date] and
  5. [Symbol] will be appended to AssetPrices:
  6.  
  7. DoCmd.SetWarnings False
  8.   MySQL = "INSERT INTO AssetPrices SELECT * FROM LivePricing;"
  9.   DoCmd.RunSQL MySQL
  10. DoCmd.SetWarnings True
Feb 21 '07 #2
billelev
119 100+
Wonderful. Thank you.
Feb 21 '07 #3
MSeda
159 Expert 100+
I think it might suit you better if you used an if statement to either insert a record if there aren’t any for the listed date and symbol or Update the record with the latest value if it is present.
Try something like


Expand|Select|Wrap|Line Numbers
  1. If isnull(dlookup(“RecordID”, “AssetPrices”, “[Symbol] = ‘” & me.symbol & “’ AND [Date] = #” & me.date & “#”)) Then
  2.  
  3. MySQL = "INSERT INTO AssetPrices SELECT * FROM LivePricing WHERE [Symbol] = ‘” & me.symbol & “’ AND [Date] = #” & me.date & “# ;"
  4.  
  5. Else
  6.  
  7. MySQL =UPDATE AssetPrices INNER JOIN LivePricing ON (LivePricing.Symbol = AssetPrices.Symbol) SET AssetPrices.Price  = LivePricing.Price WHERE AssetPrices.Symbol = ‘” & me.symbol & “’ AND [Date] = #” & me.date & “# ;"
  8. End if
  9. Docmd.runsql mysql
I’m not good at writing SQl so it may need some tweeking, But what I’m trying to show you is that your executing either an insert or an update depending on whether the symbol and date combo are already present. Using only an insert statement and the sybol date as a primary key would prevent new records from with the same key from being inserted so there would be no duplicates, but it won’t update the existing price to the current one you need an update query to do that.
Feb 21 '07 #4
NeoPa
32,534 Expert Mod 16PB
Using only an insert statement and the sybol date as a primary key would prevent new records from with the same key from being inserted so there would be no duplicates, but it won’t update the existing price to the current one you need an update query to do that.
That's absolutely right.
This is a concept that very few people get, and SQL doesn't have a built in structure for handling it :(
The routine below should do the job for you.
Expand|Select|Wrap|Line Numbers
  1. 'SaveHist saves new records to the AssetPrices table.
  2. Public Sub SaveHist()
  3.     Dim strSQL As String
  4.  
  5.     Call DoCmd.SetWarnings(False)
  6.     'This SQL may fail on a large number of records.
  7.     'That's to be expected and shouldn't be seen as a problem.
  8.     strSQL = "INSERT INTO AssetPrices " & _
  9.              "([Symbol],[Date]) " & _
  10.              "SELECT DISTINCT [Symbol],[Date] " & _
  11.              "FROM LivePricing"
  12.              'If only todays data to be included then
  13.              'replace line above with the two below.
  14.              '"FROM LivePricing " & _
  15.              '"WHERE [Date]=Date()"
  16.     Call DoCmd.RunSQL(strSQL)
  17.     strSQL = "UPDATE AssetPrices AS AP " & _
  18.              "       INNER JOIN LivePricing AS LP " & _
  19.              "       ON (AP.Symbol=LP.Symbol) " & _
  20.              "       AND (AP.Date=LP.Date) " & _
  21.              "SET AP.Price=LP.Price " & _
  22.              "WHERE (AP.Price < LP.Price)"
  23.     Call DoCmd.RunSQL(strSQL)
  24.     Call DoCmd.SetWarnings(True)
  25. End Sub
Feb 23 '07 #5
billelev
119 100+
Great. Thanks, both of you.
Mar 7 '07 #6
NeoPa
32,534 Expert Mod 16PB
Not a problem :)
Did that work for you then? All understood etc?
Mar 7 '07 #7
billelev
119 100+
It certainly did!
Mar 9 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
10
by: BuddhaBuddy | last post by:
Platform is DB2/NT 7.2.9 The table was created like this: CREATE TABLE MYTEST ( MYTESTOID bigint not null primary key, FK_OTHEROID bigint not null references other, FK_ANOTHEROID bigint not...
4
by: frank | last post by:
I posted a question before (to too many groups) and this time I am sending to this group only. I have a quick script as seen below, the file_list table has a unique field called file_name. The...
0
by: Alistair | last post by:
Hi all, I am creating a database based site that keeps track of books, who has read them and the comments they have. After a little help in M.P.I.asp.DB I managed to create a database (access...
6
by: planetthoughtful | last post by:
Hi All, I have a C# ASP.NET page that submits back to itself to insert details from a form into a database table. When / if the user refreshes the page (and gets the standard warning that POST...
4
by: FangQ | last post by:
hi I am very new to mysql. I have a question about using the "on duplicate update" clause with insert command. my table "data" has two columns, field1 and field2, where field1 is the index...
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
9
by: Oonz | last post by:
Hi Friends, How can we insert records in sorted order like consider a table No Name Phone 1 test1 12345 1 test1 23455 2 test2 68638
13
by: srinivas gandrath | last post by:
Hi all, I am having trouble to writing stored procedure to insert and update the 2 tables in sql server 2005. Here is my problem. I have 2 tables with following columns. TableName:BENEFIT_PDF...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.