如何在 Access 中创建时间序列并在查询中使用它

How to create a time series in Access and can use it in a query

事实:

我使用的是 Access 2013,但在 Access 2007 中也必须是 运行。

我有以下 3 tables:

订单:

Id      DateFrom        DateTo  
1       2014-12-01      2015-03-01
2       2014-01-02      2015-03-01
3       2015-01-03      2015-03-01

库存:

Id      Label       Amount
1       Product1    20
2       Product2    10 

订单库存:

Id      OrderId     StockId     Amount
1       1           1           10
2       2           1           5
3       2           2           5
4       3           2           5

用户输入:

问题:

可以为日期从 X 到 Y 的子查询创建 'temporary' table 吗?从用户输入来看,它看起来像这样:

dates
2015-01-01
2015-01-02
2015-01-03

我想要以下结果:

Date        StockLabel  AmountInUse     AmountAvailable
2015-01-01  Product1    10              10
2015-01-01  Product2    0               10
2015-01-02  Product1    15              5
2015-01-02  Product2    5               5
2015-01-03  Product1    15              5
2015-01-03  Product2    10              0

如果在没有 VBA 的情况下甚至可以在 Access 中查询,查询会是什么样子?

是的。创建这样的查询:

SELECT DISTINCT 
    [Tens]+[Ones] AS Factor, 
    10*Abs([Deca].[id] Mod 10) AS Tens, 
    Abs([Uno].[id] Mod 10) AS Ones
FROM 
    msysobjects AS Uno, 
    msysobjects AS Deca;

将其另存为 qdyFactor。 然后创建此查询:

SELECT DISTINCT 
    DateAdd("d",[Factor],[DateFrom]) AS Dates
FROM 
    qdyFactor
WHERE 
    qdyFactor.Factor Between 0 And DateDiff("d",[DateFrom],[DateTo]);

这将创建日期列表。 最后,使用它从您的其他表中筛选和求和。