【问题标题】:interrupted time series - data transformation - SQL Server中断的时间序列 - 数据转换 - SQL Server
【发布时间】:2020-07-10 20:11:55
【问题描述】:

我正在使用 Microsoft SQL Server 2012,如果您能提供帮助,我有 2 个问题。

我有 2 个主表:

Individual  Age  Gender
    1       66     1
    2       33     0
    3       72     1

Individual Appointment_Date Clinic
    1         10-12-2018      A 
    1         05-03-2019      A  
    1         31-08-2019      A 
    2         02-12-2018      A  
    2         16-03-2019      A  
    2         12-04-2019      A   
    2         16-05-2019      A 
    3         07-12-2018      B  
    3         12-05-2019      B 
    3         19-06-2019      B 

我想运行中断时间序列分析,以了解干预对结果指标的影响。我没有这个结果指标,我需要每周使用 Total_Appointments 来估计它。我计算了总时间中每个人的 Total_Appointments,如下所示:

SELECT T1.Individual, COUNT(DISTINCT T2.Appointment_Date) AS [TOTAL_APPOINTMENTS]
FROM T1 LEFT JOIN T2
     ON T1.Individual = T2.Individual AND
        T2.APPOINTMENT_DATE > '2018-12-01' AND
        T2.APPOINTMENT_DATE < '2019-08-31'

问题 1:我如何估算每个人和每周的 Total_Appointments?这是我需要执行的中间步骤,但我最终会得到一个在临床层面汇总的结果指标。

然后,我还需要将其他变量(Avg_Age、Avg_Gender)转换为固定的时间间隔,例如:

Date_start   Date_end   Weeks_passed Clinic Outcome Avg_Age Avg_Gender Intervention_occurs
2018-12-02   2018-12-08       1         A      0.1      63      0.7           0
2018-12-02   2018-12-08       1         B      0.3      66      0.5           0
2018-12-02   2018-12-08       1         C      0.2      67      0.6           0
2018-12-09   2018-12-15       2         A      0.2      64      0.7           0
2018-12-09   2018-12-15       2         B      0.4      65      0.6           0
2018-12-09   2018-12-15       2         C      0.3      66      0.6           0
2018-12-16   2018-12-22       3         A      0.3      64      0.7           0
2018-12-16   2018-12-22       3         B      0.3      65      0.6           0
2018-12-16   2018-12-22       3         C      0.4      66      0.6           0
2018-12-23   2018-12-29       4         A      0.5      64      0.7           1
2018-12-23   2018-12-29       4         B      0.2      65      0.6           1
2018-12-23   2018-12-29       4         C      0.3      66      0.6           1
2018-12-30   2019-01-05       5         A      0.6      64      0.7           1
2018-12-30   2019-01-05       5         B      0.5      65      0.6           1
2018-12-30   2019-01-05       5         C      0.3      66      0.6           1
2019-01-06   2019-01-12       6         A      0.6      64      0.7           1
2019-01-06   2019-01-12       6         B      0.5      65      0.6           1
2019-01-06   2019-01-12       6         C      0.3      66      0.6           1

这项研究从 2018 年 12 月 2 日(星期日)开始,直到 2019 年 8 月 31 日(星期六)。

问题 2: 您能告诉我如何在 SQL Server 中创建这样的时间序列表吗?然后我会将其导入 R 并在那里运行实际分析。

【问题讨论】:

    标签: sql-server time-series


    【解决方案1】:

    对于第 1 部分,这样做:

    with t as (
    select 1 individual, cast('20181210' as date) date, 'A' clinic union all
    select 1 individual, cast('20190305' as date) date, 'A' clinic union all
    select 1 individual, cast('20190831' as date) date, 'A' clinic union all
    select 2 individual, cast('20181202' as date) date, 'A' clinic union all
    select 2 individual, cast('20190316' as date) date, 'A' clinic union all
    select 2 individual, cast('20190412' as date) date, 'A' clinic union all
    select 2 individual, cast('20190516' as date) date, 'A' clinic union all
    select 3 individual, cast('20181207' as date) date, 'B' clinic union all
    select 3 individual, cast('20190512' as date) date, 'B' clinic union all
    select 3 individual, cast('20190619' as date) date, 'B' clinic 
    ), t1 as (
    select *, cast(datepart(year, date) as char(4)) + cast(datepart(week, date) as char(2)) yearweek from t
    )
    select count(distinct date) cnt, individual, yearweek from t1 group by individual, yearweek
    

    它创建一个使用datepart 的列yearweek,以便能够按周分组。

    现在我可以使用分析函数而不是 group by 创建整个列表。有一个小问题,因为 Microsoft Sql 不允许 count(distinct column) over (partition by ...) - 我为此使用了一种解决方法,使用了两次 dense_rank() over (...)。我还添加了一周开始专栏和一周结束专栏。 和以前一样,我使用 CTE 来首先创建示例数据,然后执行所需的修改 :

    with t as (
    select 1 individual, cast('20181210' as date) date, 'A' clinic union all
    select 1 individual, cast('20190305' as date) date, 'A' clinic union all
    select 1 individual, cast('20190831' as date) date, 'A' clinic union all
    select 2 individual, cast('20181202' as date) date, 'A' clinic union all
    select 2 individual, cast('20190316' as date) date, 'A' clinic union all
    select 2 individual, cast('20190412' as date) date, 'A' clinic union all
    select 2 individual, cast('20190516' as date) date, 'A' clinic union all
    select 2 individual, cast('20190514' as date) date, 'A' clinic union all--new row to demonstrate count-distinct
    select 3 individual, cast('20181207' as date) date, 'B' clinic union all
    select 3 individual, cast('20190512' as date) date, 'B' clinic union all
    select 3 individual, cast('20190619' as date) date, 'B' clinic 
    ), t1 as (
    select *, cast(datepart(year, date) as char(4)) + cast(datepart(week, date) as char(2)) yearweek,
    dateadd(day,   -((5 + DATEPART(dw, date) + @@DATEFIRST) % 7), date) start_of_week,
    dateadd(day,  7-((5 + DATEPART(dw, date) + @@DATEFIRST) % 7), date) end_of_week
     from t
    )
    select dense_rank() over (partition by individual, yearweek order by date) + dense_rank() over (partition by individual, yearweek order by date desc) - 1 cnt_distinct, 
    individual, yearweek, start_of_week, date, end_of_week
    from t1 
    order by yearweek, individual
    

    【讨论】:

    • 非常感谢您@casenonsensitive 的回复。不过,我无法遵循您的代码。例如 20181210 的第一个日期是什么?还有那些 1 1 1 2 2 2 2 3 3 3?我有成千上万的个人和数百家诊所,如果可能,我想避免单独指定它们。
    • 我添加了一个具有分析功能的更复杂的示例。这也可能是创建时间序列的方法。如果您还有其他问题,请告诉我。
    • 感谢您对此进行调查。为什么要从 20181210 开始?我应该改用 20181202,因为这是我的第一天吗?另外,为什么选择 1 有 3 行,选择 2 有 5 行,选择 3 有 3 行?我在每个诊所 (1,2,3) 的第一天应该是相同的。抱歉,我仍在尝试了解您的代码将如何生成我需要的表格。
    • 我只是从您的示例中获取数据,以演示查询的工作原理。当然你不需要它。只需使用您的表而不是整个select 1 ... union all ... clinic 问题是,我不知道您的表/视图是如何在您的数据库上调用的。我需要这些信息才能根据它进行查询。
    • 当然,对不起,我弄错了——我用这张表的意思是,我们每个人都有不同的日子,然后去不同的诊所。对于这 3 个人,我有那些日子,但我有成千上万的人,并且从 2018 年 2 月 12 日到 2019 年 2 月 12 日会有不同的人。我必须先创建一个总表,日期从 2018 年 2 月 12 日到 2019 年 12 月 31 日,然后用正确的个人填写表,如果这有意义吗?
    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    相关资源
    最近更新 更多