SQL Server SQL联接空日期

owfi6suc  于 2022-11-21  发布在  其他
关注(0)|答案(1)|浏览(157)

我有一个缺少日期的表。这个数据库的形成方式是,在它有一个条目的那一天,它显示累积指标。

Project      Date       Cumulative

    Project A    10/1       5
    Project A    10/2       6
    Project A    10/4       7
    Project A    10/7       8
    Project A    10/8       9

我可以将其合并到一个日期表中,以获得完整的日期范围,但我不知道如何保留以前日期的值。这可能会跳过1天或更多天。
理想表

Project      Date       Cumulative

    Project A    10/1       5
    Project A    10/2       6
    Project A    10/3       6
    Project A    10/4       7
    Project A    10/5       7
    Project A    10/6       7
    Project A    10/7       8
    Project A    10/8       9
vbkedwbf

vbkedwbf1#

假设我对你的问题理解正确,假设你有一个类似这样的表(包含给定日期的预聚合累积量):

create table projects (project varchar(50), day date, cumulative int);

其中包含以下数据:

insert projects (project, day, cumulative)
select 'project a', '2022-10-01', 5
union all
select 'project a', '2022-10-02', 6
union all
select 'project a', '2022-10-04', 7
union all
select 'project a', '2022-10-07', 8
union all
select 'project a', '2022-10-08', 9
union all
select 'project b', '2022-10-01', 5
union all
select 'project b', '2022-10-02', 6
union all
select 'project b', '2022-10-04', 7
union all
select 'project b', '2022-10-07', 8
union all
select 'project b', '2022-10-08', 9
union all
select 'project c', '2022-10-01', 1
union all
select 'project c', '2022-10-02', 2
union all
select 'project c', '2022-10-07', 12
union all
select 'project c', '2022-10-08', 15;

并且您有一个类似于下面的日期表(正如您在问题中提到的):

create table dates (day date);
insert dates (day) select '2022-10-01';
insert dates (day) select '2022-10-02';
insert dates (day) select '2022-10-03';
insert dates (day) select '2022-10-04';
insert dates (day) select '2022-10-05';
insert dates (day) select '2022-10-06';
insert dates (day) select '2022-10-07';
insert dates (day) select '2022-10-08';
insert dates (day) select '2022-10-09';
insert dates (day) select '2022-10-10';

有多种方法可以实现这一点-这里有两种可能性。这两种方法都首先创建一组数据,其中包含一组日期之间的所有日期和项目组合。
第一种方法利用APPLY操作从projects表中获取与输出集中的日期匹配或最接近的行。第二种方法假设给定项目/日期组合的累计数不能随时间减少--如果从需求的Angular 来看这是不可行的,您必须考虑利用APPLY或类似操作的其他选项,或者内部LAG操作。

-- APPLY
SELECT  pd.day, pd.project, c.cumulative
FROM    (
        SELECT  d.day, pn.project
        FROM    dbo.dates d
        CROSS APPLY
                (SELECT DISTINCT pn.project FROM dbo.projects pn) pn
        WHERE   d.day BETWEEN '2022-10-01' AND '2022-10-15'
        ) pd
OUTER APPLY
        (
        SELECT  TOP 1 p.cumulative
        FROM    dbo.projects p
        WHERE   p.project = pd.project
                AND p.day <= pd.day
        ORDER BY
                p.day DESC
        ) c
ORDER BY
        pd.day, pd.project;

-- MAX over partition
SELECT  pd.day, pd.project,
        COALESCE(p.cumulative, MAX(p.cumulative) OVER (PARTITION BY pd.project ORDER BY pd.day, pd.project)) AS cumulative
FROM    (
        SELECT  d.day, pn.project
        FROM    dbo.dates d
        CROSS APPLY
                (SELECT DISTINCT pn.project FROM dbo.projects pn) pn
        WHERE   d.day BETWEEN '2022-10-01' AND '2022-10-15'
        ) pd
LEFT JOIN
        dbo.projects p
ON      p.project = pd.project
        AND p.day = pd.day
ORDER BY
        pd.day, pd.project;

相关问题