【发布时间】:2018-06-06 17:33:20
【问题描述】:
改写:我有以下计算日期范围和预期结果,
我想在给定日期范围内分配月份的收入,其中 26 日作为月份开始日期,25 日作为月份结束日期。
看图
表
【问题讨论】:
-
Endate : 15/09/2017有错字吗? -
为 2017 年 9 月 15 日的打字错误道歉
标签: sql sql-server tsql
改写:我有以下计算日期范围和预期结果,
我想在给定日期范围内分配月份的收入,其中 26 日作为月份开始日期,25 日作为月份结束日期。
看图
表
【问题讨论】:
Endate : 15/09/2017 有错字吗?
标签: sql sql-server tsql
我认为您的样本数据和预期结果存在很多错误。猜猜你正在寻找这样的东西
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
【讨论】:
DATEFROMPARTS 在sql server 2012 中引入,2008 年不能使用。检查更新
我发现你的问题中有很多错误,很难猜出你想要什么,但我认为你正在寻找类似的东西:
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 |
+---------------------+---------------------+------------+----------------+-----------+------+
【讨论】: