sql—通过几个步骤从一个表汇总到另一个表

z0qdvdin  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(308)

我需要总结如下:
日期:开始日期:结束日期: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))

任何协助都将不胜感激。谢谢您。

eeq64g8w

eeq64g8w1#

我设法找到了答案。我创建了一个临时表,其中包含数据表的所有排列和摘要表中的日期。
唯一的问题是,当我使用真实数据(而不是上面显示的测试数据)时,临时的数据是3300万条记录,这使得它相当麻烦。
使用的代码如下:

-- Part 1 - create merged table
SELECT DISTINCT dbo.TBL_CASES.Date_Start, dbo.TBL_CASES.Date_End, dbo.TBL_CASES.Counter_val, dbo.TBL_CASES.[w/c], dbo.TBL_TOTALS.Week_Date
INTO test.dbo.merged
FROM   dbo.TBL_CASES CROSS JOIN
             dbo.TBL_TOTALS

-- I then added a field called weeks wait to the merged table

-- Part 2 - update difference
Update test.dbo.merged
Set test.dbo.merged.[Weeks_Wait]= DATEDIFF(WEEK,[Date_Start],[w/c])

-- part 3 - update totals
Update test.dbo.tbl_totals
Set [Total_Cases] = (Select sum(counter_val)
FROM   dbo.merged
Where   (test.dbo.merged.[Weeks_Wait] < 5) AND 
        (test.dbo.tbl_totals.[Week_Date] = test.dbo.merged.[Week_Date]) AND 
        (test.dbo.merged.[Weeks_Wait] between test.dbo.tbl_totals.[Week_From] and test.dbo.tbl_totals.[Week_To])
Group by test.dbo.merged.[Week_Date])

相关问题