Solved Handling data from a spreadsheet (1 Viewer)

bob fitz

AWF VIP
Local time
Today, 18:45
Joined
May 23, 2011
Messages
4,719
In my app I have a table called "tblHistoricalDataMASTER" that is derived from a spreadsheet. It has data about the price of shares e.g.
IDOpenHighLowCloseTickerStockDate2
142.049999453838.23FB5/18/12

I have written a function that steps through this table and writes the values to another table called "tblHistoricalDataMASTERTrans" that looks like this:
HistIDTickerStockDatePriceTypeStockValueMasterID
1FB5/18/12Open42.0499991
2FB5/18/12High451
3FB5/18/12Low381
4FB5/15/12Close38.231
The function does the job but looping through 200k records and creating 800k new records takes longer than I would like it to.

Can anyone suggest a quicker/better way of achieving this. Perhaps with some kind of query?
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:45
Joined
May 21, 2018
Messages
8,527
I would think you could do that in a xtab.
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 13:45
Joined
Apr 27, 2015
Messages
6,328
Good morning Bob,

I do know that Access LOVES batch queries and that Appened and Update queries run a LOT faster then looping procedures.
 
Last edited:

bob fitz

AWF VIP
Local time
Today, 18:45
Joined
May 23, 2011
Messages
4,719
Good morning Bob,

I do know that Access LOVERS batch queries and that Appened and Update queries run a LOT faster then looping procedures.
Hi NauticalGent,

Thanks for your interest. Yes, I've read many times that queries are faster than looping through recordsets with code I'm just looking for some help/guidance with creating the required query :unsure:
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 13:45
Joined
Apr 27, 2015
Messages
6,328
If it's as straightforward as it sounds, it should be as simple as linking to the spreadsheet and then doing the Append and/or updates queries. Am I misunderstanding your question?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:45
Joined
May 21, 2018
Messages
8,527
Code:
TRANSFORM First(tblStock.[StockValue]) AS FirstOfStockValue
SELECT tblStock.[MasterID], tblStock.[Ticker], tblStock.[StockDate], First(tblStock.[StockValue]) AS [Total Of StockValue]
FROM tblStock
GROUP BY tblStock.[MasterID], tblStock.[Ticker], tblStock.[StockDate]
PIVOT tblStock.[PriceType];
tblStock_Crosstab tblStock_Crosstab

MasterIDTickerStockDateTotal Of StockValueCloseHighLowOpen
1​
FB
5/18/2012​
38​
38.23​
45​
38​
42.049999​
 

bob fitz

AWF VIP
Local time
Today, 18:45
Joined
May 23, 2011
Messages
4,719
@MajP I am having problems understanding the SQL statement offered.

I have attached a copy of my db with just a few records to keep its' size small.

Would you please create the query and then post it back here.
 

Attachments

  • Database1.zip
    32.8 KB · Views: 118

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:45
Joined
May 21, 2018
Messages
8,527
I am not very good at cross tabs, but there is a wizard to do this. You pick the rows, pick the columns, and what to pivot. Select the query wizard, that is the way I usually do it.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 13:45
Joined
Feb 19, 2002
Messages
43,257
Bob, if you're starting with all the columns in a single row, the fastest solution is 4 append queries. One for each of the prices. What I don't understand is why you are turning them into four rows. The data as it comes in is properly normalized.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:45
Joined
May 21, 2018
Messages
8,527
I read this backward. I thought it started with the 4 records and wanted 1 record.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 18:45
Joined
Feb 19, 2013
Messages
16,605
you can do this with one append query

create a new table called say tblColumns
tblColumns

PKColName
1​
Open
2​
High
3​
Low
4​
Close


now create a cartesian append query

Code:
INSERT INTO tblHistoricalDataMASTERtrans ( Ticker, StockDate, PriceType, StockValue, MasterID )
SELECT Ticker, StockDate2, ColName, Choose([PK],[open],[high],[low],[close]), ID
FROM tblHistoricalDataMASTER, tblColumns
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:45
Joined
May 7, 2009
Messages
19,230
make a Union Query (qryUnion) and then Create an Append Query (qryAppend).
 

Attachments

  • Database1.zip
    34.7 KB · Views: 104

bob fitz

AWF VIP
Local time
Today, 18:45
Joined
May 23, 2011
Messages
4,719
you can do this with one append query

create a new table called say tblColumns
tblColumns

PKColName
1​
Open
2​
High
3​
Low
4​
Close


now create a cartesian append query

Code:
INSERT INTO tblHistoricalDataMASTERtrans ( Ticker, StockDate, PriceType, StockValue, MasterID )
SELECT Ticker, StockDate2, ColName, Choose([PK],[open],[high],[low],[close]), ID
FROM tblHistoricalDataMASTER, tblColumns
Thank you Chris. This works perfectly and gives me exactly what I need.
 

bob fitz

AWF VIP
Local time
Today, 18:45
Joined
May 23, 2011
Messages
4,719
Bob, if you're starting with all the columns in a single row, the fastest solution is 4 append queries. One for each of the prices.
Thanks for your interest Pat. I'll look into your suggestion, although Arne and Chris have both offered very acceptable solutions.

What I don't understand is why you are turning them into four rows. The data as it comes in is properly normalized.
The app that I'm working on needs to search through each days' stock value prices and find the date that a particular price is met.
I thought it would be better to search a single column rather than four columns.
 

bob fitz

AWF VIP
Local time
Today, 18:45
Joined
May 23, 2011
Messages
4,719
I am not very good at cross tabs, but there is a wizard to do this. You pick the rows, pick the columns, and what to pivot. Select the query wizard, that is the way I usually do it.
Thank you for your interest. I have used crosstab queries on a few occasions but only using the wizard. Oh I love that wizard ;)
 

bob fitz

AWF VIP
Local time
Today, 18:45
Joined
May 23, 2011
Messages
4,719
Choose()...never heard of it. Learn something new everyday
Thanks for your interest NauticalGent. I've never heard of it either but I've just "googled" it. It seems that it is an Excel function.
I don't use Excel and although that's where this data is coming from I'd like to deal with it in Access.
 

Users who are viewing this thread

Top Bottom