SQL访问添加记录



我在访问中有一个表/查询,并希望根据日期和月数添加新记录。

这个想法是添加记录的接下来的几个月。

例如,在我的桌子中,我有6月的值5,并且应该在3个月内有效,因此我想在7月添加一条线,并在八月的一条线中以5。

现有表

Strg     NbMonths       Value      Date    
Abc      3              5          6/1/2017 
Abc      3              8          12/1/2017 
Rtg      1              2          7/10/2017

我想拥有

Strg     NbMonths       Value      Date 
Abc      3              5          6/1/2017
Abc      3              5          7/1/2017
Abc      3              5          8/1/2017
Abc      3              8          12/1/2017
Abc      3              8          1/1/2018
Abc      3              8          2/1/2018
Rtg      1              2          7/10/2017

我在Access 2010中使用SQL。是 - 可以在SQL查询中进行?

MS Access缺乏这样做的大多数明显方法。这是使用union all的明确方法:

select t.strg, t.nbMonths, t.value, t.date
from t
union all
select t.strg, t.nbMonths, t.value, dateadd(m, 1, t.date)
from t
where t.nbMonths >= 2
union all
select t.strg, t.nbMonths, t.value, dateadd(m, 2, t.date)
from t
where t.nbMonths >= 3
union all
select t.strg, t.nbMonths, t.value, dateadd(m, 3, t.date)
from t
where t.nbMonths >= 4;

您需要一个单独的子查询,要为要扩展到的每个值。

您可以为此创建 cartesian 查询:

SELECT DISTINCT 
    T.Strg,
    T.NbMonths,
    T.Value,
    DateAdd("m", Abs([Uno].[id] Mod 10) , T.[Date]) As [Date]
FROM 
    TableStrg As T,
    MSysObjects AS Uno
WHERE 
    Abs([Uno].[id] Mod 10) < T.NbMonths

输出:

Strg NbMonths Value Date
Abc  3        5     2017-06-01
Abc  3        5     2017-07-01
Abc  3        5     2017-08-01
Abc  3        8     2017-12-01
Abc  3        8     2018-01-01
Abc  3        8     2018-02-01
Rtg  1        2     2017-07-10

最新更新