質問

I have an MS Access database which contains 2 tables; the first containing rows showing “dd/mm/yyy”, “hh:mm”, “price” – with each row showing data for the next incremental minute. Basically , intraday minute price data for a stock index.

The second table contains the following data; “dd/mm/yyy”, “Closing Price”, “volatility” – with volatility priced using a format of “15.55” for example. Each additional row shows information for the next incremental day.

So the first table has rows going up in minutes, the second has rows going up in days.

I would like to form a pyodbc query that pulls out data from the second table, based on the date and references it to the first table. So that I can get rows of data containing: “dd/mm/yy”, “hh:mm”, “price”, “closing price”, “volatility”.

The “closing price” and “volatility” values with then hopefully repeat themselves until the minutes tick over into the next day, and then they will update to show the next day’s values.

I am completely new to programming and have managed to do the following so far. I can pull each query down individually (and print them so I know what I am getting).

I know that the output below contains an additional “00:00:00” after the date – not 100% sure how to get rid of that – and I don’t think it is vital anyway as the dates should still be able to be referenced. (one problem at a time! ;))

Output from table one looks like this:

2005-01-03 00:00:00  17:00  1213.25
2005-01-03 00:00:00  17:01  1213.25
2005-01-03 00:00:00  17:02  1213.75
2005-01-03 00:00:00  17:03  1213.75

Output from table 2 looks like this:

2005-01-03 00:00:00  1206.25  14.08
2005-01-04 00:00:00  1191.00  13.98
2005-01-05 00:00:00  1183.25  14.09
2005-01-06 00:00:00  1188.25  13.58

here is my code so far:

from math import sqrt
import time
import pyodbc

"""establishes connection to the database and creates "cursors" with which to query data"""

ACCESS_DATABASE_FILE = "C:\\Python27\\Lib\\site-packages\\xy\\Apache         Python\\SandP.accdb"
ODBC_CONN_STR = 'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=%s;' % ACCESS_DATABASE_FILE
cnxn = pyodbc.connect(ODBC_CONN_STR)
cursor1 = cnxn.cursor()
cursor2 = cnxn.cursor()


"""uses cursor1 to pull out distinct rows of data, ordered by distinct date/time"""

cursor1.execute("select distinct [Intraday_values].[Date_], Time_, Close from [Intraday_values] order by [Intraday_values].[Date_]")
row1 = cursor1.fetchall()
for row1 in row1:
print row1[0], row1[1], row1[2]
time.sleep(2)

"""uses cursor2 to pull out settlement prices and volatility data and order by distinct date"""

cursor2.execute("select distinct [Closing_prices].[Date_], Last_price, Volatility from [Closing_prices] order by [Closing_prices].[Date_]")
row2 = cursor2.fetchall()
for row2 in row2:
print row2[0], row2[1], row2[2]
time.sleep(2)

Any help or suggestions would be very much appreciated and may just save me from pulling the rest of my hair out….

役に立ちましたか?

解決

It looks to me like you should be able to do that with a single query that JOINs both tables

cursor1.execute(
    "select distinct iv.[Date_], iv.Time_, iv.Close, cp.Last_price, cp.Volatility " +
    "from [Intraday_values] AS iv INNER JOIN [Closing_prices] AS cp" +
    "    ON cp.[Date_] = iv.[Date_] " +
    "order by iv.[Date_], iv.Time_")
row1 = cursor1.fetchall()
for row1 in row1:
    print row1[0], row1[1], row1[2], row1[3], row1[4]
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top