调整日期并添加新行

时间:2019-02-25 22:26:33

标签: sql ms-access

我试图计算日期之间的差异,但是为了按月正确地将它们分开,我希望添加新行,而不是每个月都有一列。我需要以下条件:

如果开始日期[ss_strt_dtd]的月份等于结束日期[ss_end_dtdd]的月份,则无需进行任何操作。

但是,如果月份不同,那么我需要开始日期保持不变,但结束日期应为新月的第一天。然后在月初的新开始日期和结束日期保持不变。

示例:

[ss_strt_dtd]01/28/2018  [ss_end_dtd]02/01/2018
[ss_strt_dtd]02/01/2018 [ss_end_dtd]02/02/2018

行看起来像这样:

$num = 5;
$location = 'tree';
$output = 'There are ' . $num . ' monkeys in the ' . $location; // There are 5 monkeys in the tree

任何想法都将不胜感激!

2 个答案:

答案 0 :(得分:0)

您可以使用UNION:

select ss_strt_dtd, ss_end_dtd from [S&S_Combined]
where year(ss_strt_dtd) = year(ss_end_dtd) and month(ss_strt_dtd) = month(ss_end_dtd)
union all
select ss_strt_dtd, DateSerial(year(ss_end_dtd), month(ss_end_dtd), 1) from [S&S_Combined]
where year(ss_strt_dtd) <> year(ss_end_dtd) or month(ss_strt_dtd) <> month(ss_end_dtd)
UNION ALL select DateSerial(year(ss_end_dtd), month(ss_end_dtd), 1), ss_end_dtd from [S&S_Combined]
where year(ss_strt_dtd) <> year(ss_end_dtd) or month(ss_strt_dtd) <> month(ss_end_dtd);

答案 1 :(得分:0)

您可以使用一组查询来解决,如果时间跨度数月,这些查询也将起作用。

首先进行小查询以返回10个数字:

SELECT DISTINCT Abs([id] Mod 10) AS N
FROM MSysObjects;

将其另存为

然后查询以日期间隔返回一系列月份:

PARAMETERS 
    [DateStart] DateTime, 
    [DateEnd] DateTime;
SELECT 
    [Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000+[Ten_6].[N]*1000000 AS Id, 
    [DateStart] AS DateStart,
    [DateEnd] AS DateEnd, 
    DateAdd("m",[Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000+[Ten_6].[N]*1000000,[DateStart]) AS DateMonth
FROM 
    Ten AS Ten_0, 
    Ten AS Ten_1, 
    Ten AS Ten_2, 
    Ten AS Ten_3, 
    Ten AS Ten_4, 
    Ten AS Ten_5, 
    Ten AS Ten_6
WHERE 
    (((DateAdd("m",
        [Ten_0].[N]+[Ten_1].[N]*10+[Ten_2].[N]*100+[Ten_3].[N]*1000+[Ten_4].[N]*10000+[Ten_5].[N]*100000+[Ten_6].[N]*1000000,
        [DateStart]))<=DateAdd("m",
        DateDiff("m", [DateStart],DateAdd("d",-1,[DateEnd])),[DateStart])) 
    AND ((Ten_0.N)<=DateDiff("m",[DateStart],[DateEnd])\1) 
    AND ((Ten_1.N)<=DateDiff("m",[DateStart],[DateEnd])\10) 
    AND ((Ten_2.N)<=DateDiff("m",[DateStart],[DateEnd])\100) 
    AND ((Ten_3.N)<=DateDiff("m",[DateStart],[DateEnd])\1000) 
    AND ((Ten_4.N)<=DateDiff("m",[DateStart],[DateEnd])\10000) 
    AND ((Ten_5.N)<=DateDiff("m",[DateStart],[DateEnd])\100000) 
    AND ((Ten_6.N)<=DateDiff("m",[DateStart],[DateEnd])\1000000));

将其另存为 MonthsDateRange

最后,根据开始日期和结束日期计算每个月的 From To 日期:

SELECT 
    MonthsDateRange.Id, 
    MonthsDateRange.DateStart, 
    MonthsDateRange.DateEnd, 
    Year([DateMonth]) AS [Year], 
    Month([DateMonth]) AS [Month], 
    IIf(DateDiff("m",[DateStart],[DateMonth])=0,
        [DateStart],
        DateSerial(Year([DateMonth]),Month([DateMonth]),1)) AS DateFrom, 
    IIf(DateDiff("m",[DateEnd],[DateMonth])=0,
        [DateEnd],
        DateSerial(Year([DateMonth]),Month([DateMonth])+1,1)) AS DateTo, 
    DateDiff("d",[DateFrom],[DateTo]) AS Days
FROM 
    MonthsDateRange;

将其另存为 DaysMonthsDateRange

它将返回如下内容:

Example output

请注意,查询被设计为能够返回数据类型 Date 的整个范围的月份。

从100-01-01到9999-12-31,共有118800条记录。