如何填写Access表中的每日日期空白?

时间:2018-05-29 15:43:02

标签: sql ms-access

我有一个名为Test的表格,其中有三列:Account, Date, and Amount。 日期如下:

Account  Date            Amount
1        25/01/2013      5000
1        20/01/2013      3000
2        25/01/2016      4000
2        20/01/2016      1000

基本上,金额仅在变化时记录。例如,对于Account 1Amount从2013年1月20日到2013年1月24日为3000,然后更改为5000,因此,为什么我们有记录。

我想完成从每个account

的第一条记录到最后一条记录的日期

例如,对于Account 1,我希望所有日期从2013年1月20日到2013年1月24日DateAmount

中的3000

我知道这是一个棘手的问题,非常感谢你的帮助。非常感谢 !!

1 个答案:

答案 0 :(得分:1)

你必须找到第一个和最后一个日期。无论如何:

首先,找到最短和最长日期:

SELECT 
    Account, 
    Min(tblProvider.[Entry Date]) AS FirstDate, 
    Max(tblProvider.[Entry Date]) AS LastDate
FROM 
    Test
GROUP BY 
    Account;

将其另存为 qDateMinMax

然后创建因子查询:

SELECT DISTINCT 
    [Thousands]+[Hundreds]+[Tens]+[Ones] AS Factor, 
    1000*Abs([Mille].[id] Mod 10) AS Thousands, 
    100*Abs([Centa].[id] Mod 10) AS Hundreds, 
    10*Abs([Deca].[id] Mod 10) AS Tens, 
    Abs([Uno].[id] Mod 10) AS Ones
FROM 
    MSysObjects AS Uno, 
    MSysObjects AS Deca,
    MSysObjects AS Centa, 
    MSysObjects AS Mille;

将其另存为 qdxFactor

现在,组装这些:

SELECT 
    qDateMinMax.Account, 
    qDateMinMax.FirstDate, 
    qDateMinMax.LastDate, 
    DateAdd("d",[Factor],[FirstDate]) AS OtherDate,
        (Select Last(Amount) 
        From Test 
        Where Test.Account = qDateMinMax.Account 
        And Test.[Entry Date] = qDateMinMax.LastDate) As LastAmount
FROM 
    qDateMinMax, 
    qdxFactor
WHERE 
    DateAdd("d",[Factor],[FirstDate]) <= [LastDate]
ORDER BY 
    qDateMinMax.Account, 
    DateAdd("d",[Factor],[FirstDate]);

对于中位数

SELECT 
    qDateMinMax.Account, 
    qDateMinMax.FirstDate, 
    qDateMinMax.LastDate, 
    DateAdd("d",[Factor],[FirstDate]) AS OtherDate, 
        (Select (Min(Amount) + Max(Amount)) / 2
        From Test 
        Where Test.Client = qDateMinMax.Account 
        And Test.[Entry Date] Between qDateMinMax.FirstDate And qDateMinMax.LastDate) AS MedAmount
FROM 
    qDateMinMax, 
    qdxFactor
WHERE 
    DateAdd("d",[Factor],[FirstDate]) <= [LastDate]
ORDER BY 
    qDateMinMax.Account, 
    DateAdd("d",[Factor],[FirstDate]);

按年份分组:

SELECT DISTINCT 
    qDateMinMax.Account, 
    Format([FirstDate],"yyyymm") AS FirstYM, 
    Format([LastDate],"yyyymm") AS LastYM, 
    Format(DateAdd("d",[Factor],[FirstDate]),"yyyymm") AS YM, 
        (Select (Min(Amount) + Max(Amount)) / 2
        From Test 
        Where Test.Client = qDateMinMax.Account 
        And Test.[Entry Date] Between qDateMinMax.FirstDate And qDateMinMax.LastDate) AS MedAmount
FROM 
    qDateMinMax, 
    qdxFactor
WHERE 
    DateAdd("d",[Factor],[FirstDate]) <= [LastDate];

包含最后一笔金额:

SELECT DISTINCT 
    qDateMinMax.Account, 
    Format([FirstDate],"yyyymm") AS FirstYM, 
    Format([LastDate],"yyyymm") AS LastYM, 
    Format(DateAdd("d",[Factor],[FirstDate]),"yyyymm") AS YM, 
        (Select (Min(Amount) + Max(Amount)) / 2
        From Test 
        Where Test.Client = qDateMinMax.Account 
        And Test.[Entry Date] Between qDateMinMax.FirstDate And qDateMinMax.LastDate) AS MedAmount, 
        (Select Amount
        From Test 
        Where Test.Client = qDateMinMax.Account 
        And Test.[Entry Date] =LastDate) AS LastAmount
FROM 
    qDateMinMax, 
    qdxFactor
WHERE 
    DateAdd("d",[Factor],[FirstDate] <= [LastDate];