Results 1 to 10 of 10
  1. #1
    nicoper is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2014
    Posts
    19

    Last Price of the Month Query?

    Hello all

    We have this database:
    Click image for larger version. 

Name:	Access Last Close of Month Query.JPG 
Views:	10 
Size:	37.6 KB 
ID:	20208



    It has Symbol on the first column, Date (daily on trading days) on the second and Close on the third.

    We want a query that shows the last CLOSE of the month for each symbol and sum the volume of the month.

    The query table would be like this:

    Symbol Date (last date of the month) Close (last date of the month) Volume (sum of whole month)

    Hereīs the sample file:

    Sample.accdb

    Can you please help? I highly appreciate it.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,643
    An aggregate (Group By) Totals query can retrieve the last date and the volume sum.

    SELECT Symbol, Year([Date]) & Month([Date]) AS YrMo, Sum([Volume]) AS SumVol FROM Tablename GROUP BY Symbol, Year([Date]) & Month([Date]);

    Retrieving the CLOSE that is associated with the last date is more difficult. Will likely involve nested subquery or domain aggregate function.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    nicoper is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2014
    Posts
    19
    Thank you very much June7.

    Is there any way to show a readable date like:
    03/2015

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,643
    Calculate year and month separately then concatenate those constructed fields in a report textbox.

    SELECT Symbol, Year([Date]) AS Yr, Month([Date]) AS Mo, Sum([Volume]) AS SumVol FROM Tablename GROUP BY Symbol, Year([Date]), Month([Date]);

    For retrieving the entire record that is associated with the last date for each symbol, review http://allenbrowne.com/subquery-01.html#TopN
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    nicoper is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2014
    Posts
    19
    Awesome thanks. Rep given.
    Will take a look at the link.

  6. #6
    nicoper is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2014
    Posts
    19
    Gosh, itīs looking like Chinese to me.

    Iīm using MLD Downloader. There is a function there to download the monthly EOD data directly from yahoo.
    But the problem are the splits: It seems that you can only download the correct split adjusted data on the daily, returning incorrect data for the monthly.
    So the way to fix it is downloading the daily and use an Access query that gets the last day of the month "Close".

    Isnīt there an easier way to get it?

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,643
    I forgot to include expression to return last date:

    SELECT Symbol, Year([Date]) AS Yr, Month([Date]) AS Mo, Sum([Volume]) AS SumVol, Max([Date]) AS LastDate FROM Tablename GROUP BY Symbol, Year([Date]), Month([Date]);


    As noted, retrieving the Close that is associated with the last date for each month for each symbol is more difficult. The TOP N query demonstrated in the link is maybe the easiest method. However, it requires a unique record identifier field - an autonumber field will serve that purpose.

    SELECT ID, Symbol, [Date], Close FROM EOD WHERE ID IN (SELECT TOP 1 ID FROM EOD AS Dupe WHERE Dupe.Symbol=EOD.Symbol AND Year(Dupe.[Date])=Year(EOD.[Date]) AND Month(Dupe.[Date])=Month(EOD.[Date]) ORDER BY Dupe.Symbol, Dupe.[Date] Desc);

    Another method uses domain aggregate functions.

    Both of those methods may perform slowly with large datasets.

    Another:

    Query1:
    SELECT EOD.SYMBOL, Max(EOD.Date) AS MaxOfDATE, Sum(EOD.VOLUME) AS SumOfVOLUME, Year([Date]) AS Yr, Month([Date]) AS Mo
    FROM EOD
    GROUP BY EOD.SYMBOL, Year([Date]), Month([Date]);

    Query2:
    SELECT EOD.SYMBOL, EOD.DATE, EOD.CLOSE, Query1.SumOfVOLUME, Query1.Yr, Query1.Mo
    FROM Query1 INNER JOIN EOD ON (Query1.MaxOfDATE = EOD.DATE) AND (Query1.SYMBOL = EOD.SYMBOL);

    The two queries as nested all-in-one:
    SELECT EOD.SYMBOL, EOD.DATE, EOD.CLOSE, Query1.SumOfVOLUME, Query1.Yr, Query1.Mo
    FROM (SELECT EOD.SYMBOL, Max(EOD.Date) AS MaxOfDATE, Sum(EOD.VOLUME) AS SumOfVOLUME, Year([Date]) AS Yr, Month([Date]) AS Mo
    FROM EOD
    GROUP BY EOD.SYMBOL, Year([Date]), Month([Date])) AS Query1 INNER JOIN EOD ON (Query1.MaxOfDATE = EOD.DATE) AND (Query1.SYMBOL = EOD.SYMBOL);

    This seems to run much faster.


    Date is a reserved word. Should not use reserved words as names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    nicoper is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2014
    Posts
    19
    Oh man, thatīs perfect!
    And fast too, I tested it with over 1 mil rows and lasted only a few secs.
    Iīm planning on having around 8/9 mil including the monthly from 2011 till date for all relevant symbols.
    It shouldnīt be a problem since Iīd only update it once a month. The rest is done in excel.

    About the Date word, I know, Iīve been told that. But itīs been working ok and is used everywhere else in the data model. Replacing it means redoing everything (big pain).

    Man, a huge thanks from me. Best luck!

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,643
    Using reserved words can cause issues. Happened to me. An expression in query would not recognize the field name, insisted on seeing the word with it's special meaning, even surrounding with [] did not help. I don't remember the word or the expression - just remember hitting a wall.

    I use Rick Fisher's Find & Replace add-in for global edit to change a field name. Saved my sanity more than once.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    nicoper is offline Novice
    Windows 7 64bit Access 2013
    Join Date
    Sep 2014
    Posts
    19
    I never thought of that. I have a very neat tool called "Simple Search/Replace" from RJLSoftware, but it may not work with access/excel files.
    Thanks for the recommendation, will take a look. Iīm backing up everything, scared to death to have to rebuild everything.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 02-18-2015, 08:28 AM
  2. Replies: 5
    Last Post: 06-17-2014, 12:11 AM
  3. Replies: 6
    Last Post: 05-05-2014, 10:51 PM
  4. Item without price, or duplicate price
    By Auto in forum Reports
    Replies: 5
    Last Post: 07-29-2013, 09:46 PM
  5. Replies: 5
    Last Post: 07-29-2011, 12:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums