我需要总结如下:
日期:开始日期:结束日期:10/01/202012/01/2020108/01/202009/01/2020127/03/202003/04/2020112/02/2020118/02/202020/02/2020101/01/202008/01/2020130/12/201907/01/2020101/01/202011/01/20201
汇总表如下:
周从周到日总病例0113/01/20201313/01/20203200013/01/20200112/04/20201312/04/20203200012/04/2020
每行的第一步是仅使用数据表中的行,其中结束日期早于摘要表中的周日期。
例如,在汇总表中使用13/1/2020时,数据表中有5行的结束日期在前面。
然后,我需要根据数据表中week\u date字段inteh summary table和date\u start字段之间的周差对数据进行分组。
例如,数据表中有两行的“结束日期”字段早于13/1/20,并且13/1/20与开始日期之间的周差小于1。
要创建数据表,请尝试:
CREATE TABLE TEST.DBO.TBL_CASES
(
Date_Start DATE,
Date_End DATE,
Counter_val FLOAT
);
INSERT INTO TEST.DBO.TBL_CASES (Date_Start,Date_End,Counter_val)
VALUES (cast('1/1/2020' as date), cast('01/28/2020' as date), 1),
(cast('1/1/2020' as date), cast('01/09/2020' as date), 1),
(cast('1/1/2020' as date), cast('04/03/2020' as date), 1),
(cast('1/1/2020' as date), cast('02/16/2020' as date), 1),
(cast('1/1/2020' as date), cast('02/20/2020' as date), 1),
(cast('1/1/2020' as date), cast('01/08/2020' as date), 1),
(cast('1/1/2020' as date), cast('01/07/2020' as date), 1),
(cast('1/1/2020' as date), cast('01/11/2020' as date), 1);
CREATE TABLE TEST.DBO.TBL_TOTALS
(
Week_From FLOAT,
Week_To FLOAT,
Week_Date DATE,
Total_Cases FLOAT
);
INSERT INTO TEST.DBO.TBL_TOTALS (Week_From, Week_To, Week_Date, Total_Cases)
VALUES (0, 1, cast('1/13/2020' as date), 0),
(1, 3, cast('1/13/2020' as date), 0),
(3, 2000, cast('1/13/2020' as date), 0),
(0, 1, cast('4/12/2020' as date), 0),
(1, 3, cast('4/12/2020' as date), 0),
(3, 2000, cast('4/12/2020' as date), 0);
我尝试了以下操作,但出现错误:
DECLARE @var_week_in_question DATE
DECLARE @var_date_start DATE
DECLARE @var_date_end DATE
DECLARE @var_week_total float
DECLARE @var_Week_From float
DECLARE @var_Week_to Float
UPDATE TEST.DBO.TBL_TOTALS
UPDATE TEST.DBO.TBL_CASES
SET @week_in_question = TEST.DBO.TBL_TOTALS.Week_Date,
@var_date_start = TEST.DBO.TBL_CASES.Date_start,
@var_date_end = TEST.DBO.TBL_CASES.Date_end,
@var_Week_From = TEST.DBO.TBL_TOTALS.Week_From,
@var_Week_To = TEST.DBO.TBL_TOTALS.Week_To,
@var_week_total = TEST.DBO.TBL_TOTALS.Total_Cases = (SELECT SUM(TEST.DBO.TBL_CASES.Counter_val)
FROM TEST.DBO.TBL_CASES
WHERE (@var_date_end < @week_in_question)
AND (DATEDIFF(week, @week_in_question, @var_date_start) > @var_Week_From)
AND (DATEDIFF(week, @week_in_question, @var_Date_start) <= @var_Week_To))
任何协助都将不胜感激。谢谢您。
1条答案
按热度按时间eeq64g8w1#
我设法找到了答案。我创建了一个临时表,其中包含数据表的所有排列和摘要表中的日期。
唯一的问题是,当我使用真实数据(而不是上面显示的测试数据)时,临时的数据是3300万条记录,这使得它相当麻烦。
使用的代码如下: