Domanda

I have some data is that is daily (day on day) closing figures for a tracked supply and is in one MS Access table that has 2 columns - Dates (the date), PXLast(the day's closing figure)). I have daily data from Jan 1991 to Aug 2013 and I wanted to get the percentage change of PXLast at every year end compared to last year year end as follows:

Year | Percentage Change of PXLast(Year on Year)

1991 | 15.2% 1992 | 9.2%

The year end date varies (not always 31st ) and I am going about getting the last PXLast value by:

1.Get the max date in Dec every year: results in MyYear, MyMonth, MyDay

2.Combine it using DateSerial(MyYear, MyMonth, MyDay)

3.Join the resulting query to the table and inner join on the date column

4.Get the PXLast value

   SELECT EndDates.EndDates, NSE20.PX_LAST AS LookPoint
    FROM NSE20 INNER JOIN 
    (SELECT DateSerial([MyYear],[MyMonth],[MyDay]) 
    AS EndDates FROM (SELECT 12 AS MyMonth, MyDay, MyYear FROM 
    (SELECT Max(Day([Dates])) AS MyDay, Year([Dates]) AS MyYear 
    FROM NSE20 WHERE (((Month([Dates]))=12)) 
     GROUP BY Year([Dates])) AS EndYearValues) 
    AS EndValueDates) 
    AS EndDates ON NSE20.Dates = EndDates.EndDates;

Could anyone assist me get the corresponding value using a query for previous year end

eg for 29 Dec 2006, it should show the current value and show the value for 31 Dec 2005

in the same row ie

Year | Current Year End| Previous Year End

2005 | 3449.00 | 4611.19

2006 | 9.2% |3449.00

Any help is appreciated.

Any suggestions to a better way of doing this is very very welcome....

È stato utile?

Soluzione

Let's assume that you have some test data in a table named [NSE20] that looks like this

Dates       PXLast
----------  ------
2010-07-01     131
2010-12-31     130
2011-11-12     123
2011-12-30     125
2012-01-03     127
2012-12-31     129

I'd start by creating a saved query in Access named [NSE20_year_ends] that identifies the year-end dates by (calendar) year:

SELECT Year(Dates) AS CalendarYear, Max(Dates) AS YearEndDate
FROM NSE20
GROUP BY Year(Dates)

That will produce

CalendarYear  YearEndDate
------------  -----------
        2010  2010-12-31 
        2011  2011-12-30 
        2012  2012-12-31 

Then I'd create another saved query named [NSE20_year_end_balances] to extract the closing balances for each year:

SELECT NSE20_year_ends.CalendarYear, NSE20.PXLast
FROM 
    NSE20
    INNER JOIN
    NSE20_year_ends
        ON NSE20.Dates = NSE20_year_ends.YearEndDate

That will give us

CalendarYear  PXLast
------------  ------
        2010     130
        2011     125
        2012     129

Now we can do a self-join on that query to calculate the percentage change

SELECT 
    y1.CalendarYear, 
    (y1.PXLast - y0.PXLast) / y0.PXLast * 100 AS PctChange
FROM
    NSE20_year_end_balances y1
    INNER JOIN
    NSE20_year_end_balances y0
        ON y0.CalendarYear = y1.CalendarYear - 1

resulting in

CalendarYear  PctChange        
------------  -----------------
        2011  -3.84615384615385
        2012                3.2
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top