在mysql

时间:2019-04-16 06:07:07

标签: mysql lag partition

我正在尝试创建MySQL数据集,该数据集也具有该用户ID的先前研究(外科研究中第一个研究的值为1)的条目。每个用户可以进行多个检查。我正在尝试使用滞后函数来获取以前的研究数据。尝试使用滞后函数时,我得到研究1的重复值。


WITH Complete_Data AS (
    SELECT *
    FROM Data
    GROUP BY UserId, studyno
)
SELECT *, LAG(indication_of_failure, 1)
OVER (
        PARTITION BY UserId
        ORDER BY studyno
    ) prev_study_indication_of_failure
    , 
    LAG(BCVA_State,1)
    OVER (
        PARTITION BY UserId
        ORDER BY studyno
    ) prev_study_BCVA
FROM 
    Complete_Data
    GROUP BY UserId, studyno
    ;

我期望当studyno = 1时prev_study_indication_of_failure和prev_study_BCVA中为空,但当studyno> = 2时将填充值。

由于研究编号= 1的重复条目,我获得了prev_study_indication_of_failure和prev_study_BCVA中的值


样本数据

The following image gives the actual data sample

预期结果 The following image gives the expected outcome

0 个答案:

没有答案