我在计算日期方面遇到了一些困难。让我们想象一个包含多个患者(超过数千名)的数据集,我将仅呈现一个患者,作为列 [patent_ID]。请记住此场景的相交/重叠逻辑。 对于该患者,我需要根据之间的列(match_key)的匹配来计算两个事件之间的时间。例如,当匹配项为 2234 时,在该匹配项中获取日期 2,然后在日期 1 中扣除相同的匹配键。以下是所需输出的示例:
patient_ID match_key Date1 Date2 Desired_column
patient5 2234 21/10/2023 14:34 21/10/2023 14:36 21/10/2023 14:55 - 21/10/2023 14:34
patient5 7889 21/10/2023 14:34 21/10/2023 14:38 21/10/2023 15:45 - 21/10/2023 14:34
patient5 21/10/2023 16:17 21/10/2023 14:40
patient5 2234 21/10/2023 14:34 21/10/2023 14:55
patient5 21/10/2023 16:45 21/10/2023 15:36
patient5 7889 21/10/2023 17:03 21/10/2023 15:45
我可以看到一种将列添加到具有非空匹配键的每一行的方法:
(If([match_key] is not null,Max([Date2]) over ([patient_ID],
[match_key]),null)) - (If([match_key] is not null,Min([Date1]) over
([patient_ID],[match_key]),null))
第一部分:
(If([match_key] is not null,Max([Date2]) over ([patient_ID],
[match_key]),null))
计算病人和match_key的最大Date2,第二部分:
(If([match_key] is not null,Min([Date1]) over
([patient_ID],[match_key]),null))
返回患者和 match_key 的最小 Date2。
我看不到一种只为每个匹配键的第一行返回它的方法,至少不能一次性返回。这是要求的一部分吗?