1

我有一些数据是跟踪供应的每日(每日)收盘数字,并且在一个 MS Access 表中,该表有 2 列 - 日期(日期),PXLast(当天的收盘数字))。我有从 1991 年 1 月到 2013 年 8 月的每日数据,我想得到 PXLast 在每年年底与去年年底相比的百分比变化如下:

年份 | PXLast的百分比变化(同比)

1991 | 15.2% 1992 | 9.2%

年终日期会有所不同(并不总是 31st ),我将通过以下方式获取最后一个 PXLast 值:

1.获取每年12月的最大日期:MyYear,MyMonth,MyDay中的结果

2.使用 DateSerial(MyYear, MyMonth, MyDay) 组合它

3.将生成的查询加入到表中并在日期列上进行内连接

4.获取PXLast值

   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;

任何人都可以帮助我使用去年年底的查询获得相应的值

例如 2006 年 12 月 29 日,它应该显示当前值并显示 2005 年 12 月 31 日的值

在同一行,即

年份 | 本年度结束| 上年末

2005 | 3449.00 | 4611.19

2006 | 9.2% |3449.00

任何帮助表示赞赏。

非常欢迎任何关于更好的方法的建议......

4

1 回答 1

1

假设您在名为 [NSE20] 的表中有一些测试数据,如下所示

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

我首先在 Access 中创建一个名为 [NSE20_year_ends] 的已保存查询,该查询按(日历)年份标识年终日期:

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

那会产生

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

然后我会创建另一个名为 [NSE20_year_end_balances] 的已保存查询来提取每年的期末余额:

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

那会给我们

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

现在我们可以对该查询进行自联接来计算百分比变化

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

导致

CalendarYear  PctChange        
------------  -----------------
        2011  -3.84615384615385
        2012                3.2
于 2013-10-07T16:24:57.800 回答