簡體   English   中英

SQL中如何獲取12個月的連續數據

[英]How to get 12 months continuous data in SQL

我想獲取連續接受服務 12 個月的患者的數據。 在我的數據中,我有 Patient_ID 和 service_date 字段。

我嘗試了下面的查詢,但結果不正確。

select a.*, count(distinct b.svcdate) over(partition by a.patientid, a.svcdate) as events,
count(month(SVCDATE)) over (partition by patientid, SVCDATE) as month_count
from iv_lasix_hf_data_pull as a
left join iv_lasix_hf_data_pull as b
on a.patientid = b.patientid
and b.svcdate >= a.svcdate
and b.svcdate <= dateadd(month, 12, a.svcdate) and month_count = 12;

謝謝您的幫助!!

select a.*, ROW_NUMBER() OVER(ORDER BY month(svcdate) AS ROW_NUMBER, DATEDIFF(M, ROW_NUMBER() OVER(ORDER BY month(svcdate)), svcdate) AS Diff from iv_lasix_hf_data_pull 作為左加入 iv_lasix_hf_data_pull 作為 b a.patientid = b.patientid and b.svcdate >= a.svcdate and b.svcdate <= dateadd(month, 12, a.svcdate) and month_count = 12;

如果月份是連續的,上述查詢將在 Diff 中為您提供相同的數字。 可以根據需要分組統計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM