【问题标题】:Split Date range over the months considering 26th as first day of month and 25th as last day of month考虑到 26 日为每月的第一天和 25 日为每月的最后一天的月份拆分日期范围
【发布时间】:2018-06-06 17:33:20
【问题描述】:

改写:我有以下计算日期范围和预期结果,
我想在给定日期范围内分配月份的收入,其中 26 日作为月份开始日期,25 日作为月份结束日期。 看图

【问题讨论】:

  • Endate : 15/09/2017 有错字吗?
  • 为 2017 年 9 月 15 日的打字错误道歉

标签: sql sql-server tsql


【解决方案1】:

我认为您的样本数据和预期结果存在很多错误。猜猜你正在寻找这样的东西

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 

【讨论】:

  • 嗨 Pitsu,非常感谢我正在寻找的东西,如果你能详细说明功能,我正在使用 SQL 2008 r2 Datefromparts(Year(@startdate), Month(@startdate) - 1, 26);
  • @Ali - DATEFROMPARTSsql server 2012 中引入,2008 年不能使用。检查更新
  • 嗨,你能检查一下我是否已经改写了这个问题,实际上我只想将月份视为 26 日(月份开始日期)和 25 日(月份结束日期)。
【解决方案2】:

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

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 |
+---------------------+---------------------+------------+----------------+-----------+------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2013-09-22
    • 1970-01-01
    • 2018-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多