分割日期范围在几个月内,第26天为第一天,第25天为月末

时间:2017-12-25 13:18:56

标签: sql sql-server tsql

改述:我有以下计算日期范围和预期结果,
我希望在给定日期范围内将收入分成几个月,第26个月作为月份开始日期,25日作为月末日期。 见图片

enter image description here

2 个答案:

答案 0 :(得分:0)

我相信您的样本数据和预期结果存在许多错误。猜猜你在寻找像这样的东西

DECLARE @startdate DATE = '2017-07-14',
        @enddate   DATE = '2017-09-15'

SET @startdate = Datefromparts(Year(@startdate), Month(@startdate) - 1, 26);

WITH cte
     AS (SELECT FirstDayOfMonth = @startdate,
                LastDayOfMonth = Dateadd(dd, -1, Dateadd(month, 1, @startdate))
         UNION ALL
         SELECT Dateadd(MONTH, 1, FirstDayOfMonth),
                EndDate = CASE
                            WHEN Dateadd(MONTH, 1, LastDayOfMonth) > @enddate THEN @enddate
                            ELSE Dateadd(MONTH, 1, LastDayOfMonth)
                          END
         FROM   cte
         WHERE  FirstDayOfMonth < Dateadd(month, -1, @enddate))
SELECT FirstDayOfMonth,
       LastDayOfMonth,
       [no-of-days] = datediff(dd,FirstDayOfMonth,LastDayOfMonth) + 1,
       Period = format(LastDayOfMonth,'MMM,yyyy'),
       Year = year(FirstDayOfMonth)
FROM   cte 

结果:

╔═════════════════╦════════════════╦════════════╦══════════╦══════╗
║ FirstDayOfMonth ║ LastDayOfMonth ║ no-of-days ║  Period  ║ Year ║
╠═════════════════╬════════════════╬════════════╬══════════╬══════╣
║ 2017-06-26      ║ 2017-07-25     ║         30 ║ Jul,2017 ║ 2017 ║
║ 2017-07-26      ║ 2017-08-25     ║         31 ║ Aug,2017 ║ 2017 ║
║ 2017-08-26      ║ 2017-09-15     ║         21 ║ Sep,2017 ║ 2017 ║
╚═════════════════╩════════════════╩════════════╩══════════╩══════╝

旧版本

DECLARE @startdate DATE = '2017-07-14',
        @enddate   DATE = '2017-09-15'

SET @startdate = cast(cast(Year(@startdate) AS CHAR(4))+'-'+cast(Month(@startdate) - 1 AS varchar(2))+'-'+ '26' AS date);
WITH cte
     AS (SELECT FirstDayOfMonth = @startdate,
                LastDayOfMonth = Dateadd(dd, -1, Dateadd(month, 1, @startdate))
         UNION ALL
         SELECT Dateadd(MONTH, 1, FirstDayOfMonth),
                EndDate = CASE
                            WHEN Dateadd(MONTH, 1, LastDayOfMonth) > @enddate THEN @enddate
                            ELSE Dateadd(MONTH, 1, LastDayOfMonth)
                          END
         FROM   cte
         WHERE  FirstDayOfMonth < Dateadd(month, -1, @enddate))
SELECT FirstDayOfMonth,
       LastDayOfMonth,
       [no-of-days] = Datediff(dd, FirstDayOfMonth, LastDayOfMonth)
                      + 1,
       Period = Cast(Datename(month, LastDayOfMonth) AS CHAR(3))+','+ Cast(Year(LastDayOfMonth) AS CHAR(4)),
       Year = Year(FirstDayOfMonth)
FROM   cte 

答案 1 :(得分:0)

我很难猜到你想要什么,因为我在你的问题中看到很多错误,但我认为你正在寻找类似的东西:

CREATE TABLE DATA (
    StartDate DATE,
    EndDate DATE,
    DailyRate INT
    );

INSERT INTO DATA VALUES
('2017-06-28', '2017-07-25', 50)

SELECT D.StartDate AS FirstDayOfMonth,
       D.EndDate AS LastDayOfMonth,
       DATEDIFF(DAY, StartDate, EndDate) AS No_Of_Days,
       CAST((DATEDIFF(DAY, StartDate, EndDate) * DailyRate) AS DECIMAL(18,2)) AS MonthlyRevenue,
       DATENAME(Month, EndDate)+ ',' + CAST(DATEPART(Year,EndDate) AS VARCHAR) AS Period,
       DATEPART(Year, EndDate) AS [Year]
FROM DATA AS D;

结果:

+---------------------+---------------------+------------+----------------+-----------+------+
|   FirstDayOfMonth   |   LastDayOfMonth    | No_Of_Days | MonthlyRevenue |  Period   | Year |
+---------------------+---------------------+------------+----------------+-----------+------+
| 28.06.2017 00:00:00 | 25.07.2017 00:00:00 |         27 |        1350,00 | July,2017 | 2017 |
+---------------------+---------------------+------------+----------------+-----------+------+