我被要求创建一个存储过程,显示可变日期范围内的租赁项目数量。我有一个包含以下架构的表:
--Note that this is condensed, and in reality has proper constraints
--and more columns. Many dates from this table are tied to a single
--ContractDetail (separate table) by ContractDetailId.
CREATE TABLE RentalContractDates
(
RentalDateId INT IDENTITY(1,1) NOT NULL, --PK
ContractDetailId INT NOT NULL, --FK
RentalDate DATETIME NOT NULL,
Quantity DECIMAL(20,8) NULL
);
INSERT INTO RentalContractDates (ContractDetailId, RentalDate, Quantity)
VALUES (1, '04/01/2016 3:00 PM', 10),
(1, '04/10/2016 1:00 PM', 2),
(1, '04/15/2016 11:00 AM', -5),
(1, '04/15/2016 11:30 AM', -2),
(1, '04/27/2016 2:00 PM', -5);
用户将输入要搜索的日期范围,并且该程序应查找属于此范围内的所有日期,然后还计算截止时间,以便客户在其租赁的另一天收取费用。
示例场景:全局截止时间设置为12:00 PM。我有2016年4月1日下午3:00租用的10个小工具。这基本上我的意思是我实际上是在2016年4月2日租用它们,因为它已经过了04/01的截止时间。我在2016年4月4日下午1:00再租2个,所以基本上是4/11/2016。我在2016年4月15日上午11:00返回5个小部件,在上午11:30返回2个小部件。我想在2016年4月27日退回所有小部件,但是我到达截止时间中午12:00,所以我实际上将收取4/02的费用,而不是4 / 02-4 / 27的费用。 -4/28。
重要提示:如果我之前在04/01之前租用了数量,这是报告范围的开头,我需要在报告中包含这些数量。例如,如果我在3/31,4/1和12上有12个租金,那么他们的总数将增加12个。换句话说,任何先前的数量都需要计算到输入报告@BeginDate和@EndDate参数的总和中。 所以04/01将会读到12,04 / 02会读22等等。
正如您所看到的,我不需要用户每天输入他们的租金,我只是让他们设置一个数量的租金的开始日期和时间,下次他们输入日期/时间组合时,它会重新总结。
当前代码:我想将此查询与整个月份的日历日期列表相关联,并相应地设置其数量。
DECLARE @BeginDate DATETIME = '04/01/2016',
@EndDate DATETIME = '04/28/2016';
DECLARE
@CutoffTime TIME = '12:00 PM';
SET @BeginDate = @BeginDate + @CutoffTime;
SET @EndDate = @EndDate + @CutoffTime;
SELECT gbd.ContractDetailId,
gbd.RentalDate,
gbd.Cutoff,
gbd.Quantity,
'Running Total' = SUM(Quantity) OVER (PARTITION BY ContractDetailId, RentalDate, Cutoff ORDER BY RentalDate)
FROM (
SELECT
r.ContractDetailId,
'RentalDate' = CONVERT(Date, RentalDate),
r2.Cutoff,
r.Quantity
FROM RentalContractDates r
INNER JOIN
(
SELECT
rcd.ContractDetailId,
'Cutoff' = CASE WHEN CONVERT(TIME, RentalDate) >= @CutoffTime THEN 'AFTER CUTOFF' ELSE 'BEFORE CUTOFF' END
FROM
RentalContractDates rcd
) r2
ON r2.ContractDetailId = r.ContractDetailId
WHERE
r.RentalDate Between @BeginDate and @EndDate
GROUP BY r.ContractDetailId, CONVERT(DATE, RentalDate), r2.Cutoff, Quantity
) gbd
ORDER BY RentalDate, Cutoff DESC
我想将此数据加入此CTE并为每个日期设置数量:
;WITH T([Date]) AS
(
SELECT @StartDate
UNION ALL
SELECT DATEADD(DAY,1,T.[Date]) FROM T WHERE T.[Date] < @EndDate
)
SELECT * FROM T
预期的最终结果: 完成后,报告最终会看起来像这样,虽然它将被转动并包含名称中的星期几:
ContractDetailId RentalDate Quantity
----------------------------------------------------------------
1 04/01/2016 0 -- 0, because rentals were input after cutoff.
1 04/02/2016 10
1 04/03/2016 10 -- Continues until 4/10
1 04/10/2016 10
1 04/11/2016 12 -- Continues until 4/15
1 04/15/2016 5 -- I returned 5 and then 2, so this should sum since both were before the cutoff time.
-- Continues until 4/27.
1 04/27/2016 5 -- 5, because -5 was entered past cutoff on 4/27.
1 04/28/2016 0
我有旋转代码以及最终输出所需的动态sql已经完成(如果需要,我可以发布这个),但我对如何通过预截止/截止后正确分组这些数据感到迷茫,并改变了相应的一天。我该如何处理这种情况?感谢您的任何建议/帮助!
修改1:修正了不正确的样本数据。
答案 0 :(得分:0)
--Inputs for your function
DECLARE @BeginDate DATE = '04/01/2016',
@EndDate DATE = '04/28/2016',
@ContractDetailID INT = 1;
--Defined in the function
DECLARE @CutoffTime TIME = '12:00 PM';
DECLARE @PriorSum DECIMAL(20,8) = 0;
DECLARE @RowCount INT = DATEDIFF(dd,@BeginDate,@Enddate) +1;
--Get Any quantities before Begin Date
SELECT @PriorSum=COALESCE(SUM(rcd.Quantity),0)
from RentalContractDates rcd
WHERE CAST(CASE when CAST(rcd.RentalDate as TIME) > @CutoffTime THEN DATEADD(dd,1,rcd.RentalDate) ELSE rcd.RentalDate end as date) < @BeginDate
AND @ContractDetailID = rcd.ContractDetailId
--Create the Days for the report
;WITH RecursiveRowGenerator (Row#, Iteration) AS (
SELECT 1, 1
UNION ALL
SELECT Row# + Iteration, Iteration * 2
FROM RecursiveRowGenerator
WHERE Iteration * 2 < CEILING(SQRT(@RowCount+1))
UNION ALL
SELECT Row# + (Iteration * 2), Iteration * 2
FROM RecursiveRowGenerator
WHERE Iteration * 2 < CEILING(SQRT(@RowCount+1))
)
, SqrtNRows AS (
SELECT *
FROM RecursiveRowGenerator
UNION ALL
SELECT 0, 0
)
, Rowtbl as (
SELECT top (@RowCount+1) A.Row# * POWER(2,CEILING(LOG(SQRT(@RowCount+1))/LOG(2))) + B.Row# as RowNum
FROM SqrtNRows A, SqrtNRows B
ORDER BY A.Row#, B.Row#
)
,
DateTable as (
select top (@RowCount) DATEADD(dd,RowNum,@BeginDate) AS ReportDate
from Rowtbl
where RowNum <= @RowCount
)
,
--Merge the days for the report with the actual rental data
GBD AS
( SELECT
@ContractDetailID as ContractDetailID,
DT.ReportDate AS 'RentalDate',
CASE when CAST(rcd.RentalDate as TIME) > @CutoffTime THEN 'AFTER CUTOFF' ELSE 'BEFORE CUTOFF' END AS 'Cutoff',
COALESCE(rcd.Quantity,0) AS Quantity
FROM DateTable DT
LEFT JOIN RentalContractDates rcd on
DT.ReportDate = CAST( CASE when CAST(rcd.RentalDate as TIME) > @CutoffTime THEN DATEADD(dd,1,rcd.RentalDate) ELSE rcd.RentalDate END as DATE)
AND @ContractDetailID = rcd.ContractDetailId
WHERE DT.ReportDate Between @BeginDate and @EndDate
)
--Final Select
SELECT gbd1.ContractDetailId,
gbd1.RentalDate,
(select SUM(gbd2.Quantity) from GBD GBD2 where GBD1.rentaldate >= GBD2.RentalDate) + @PriorSum AS RunningTotal
FROM GBD gbd1
GROUP BY gbd1.ContractDetailId,gbd1.RentalDate
ORDER BY gbd1.RentalDate asc
编辑: 递归行生成器是由Aaron Friel创建的,是我用于生成行t-sql select get all Months within a range of years的解决方案。它为所有日期生成行,因此如果没有该日期的记录,我们可以将数量加0或0。使用此生成器,从2000年到2016年生成日期非常便宜。将所有数量和之前的金额加入到那些日期是相当昂贵的。将它们聚合在一起,即使对于没有在RentalContractDates中记录的日期,您也有一个运行总计是昂贵的部分。
处理截止时间问题的部分是:
gbd.RentalDate = CAST(CASE WHEN CAST(rcd.RentalDate AS TIME) > @CutoffTime THEN DATEADD(dd, 1, rcd.RentalDate)
ELSE rcd.RentalDate END AS DATE)
它将rentaldate转换为仅限时间,与cutoffTime进行比较,如果过去则添加一天,然后仅转换为日期。