SQL 按月检查 ID 的状态变化

SQL check change in status for ID month by month

我正在尝试每月查找患者风险级别的变化。使用下面的数据。我想看看 - 一个月内有多少患者的风险水平有所增加。例如:在下面的 table 中,John 截至 05/09/2021 的风险级别为 'Low',而风险在 05/10/2021 增加到 'High'。因此,如果我为风险增加的患者创建条形图,我会在 5 月算上 John

+----+------------+------------+----------------+
| ID | MemberName | Risk level | Discharge Date |
+----+------------+------------+----------------+
| 1  | John Doe   | Low        | 03/05/2021     |
+----+------------+------------+----------------+
| 1  | John Doe   | Medium     | 05/10/2021     |
+----+------------+------------+----------------+
| 1  | John Doe   | High       | 06/10/2021     |
+----+------------+------------+----------------+
| 2  | Sam        | Medium     | 05/10/2021     |
+----+------------+------------+----------------+
| 2  | Sam        | Low        | 05/20/2021     |
+----+------------+------------+----------------+

查询

SELECT [ID], [MemberName], [Risk level], [Discharge Date],
DATEADD(month, DATEDIFF(month, 0,  [Discharge Date]), 0) as StartOfMonth,
COUNT(*) OVER (PARTITION BY ID, MONTH([Discharge Date])) as Increase_Level
--Decrease level
from Member_Risk

预期输出

+----+------------+--------------+------------------------+------------------------+
| ID | MemberName | StartOfMonth | Increase_In_Risk_Level | Decrease_In_Risk_Level |
+----+------------+--------------+------------------------+------------------------+
| 1  | John Doe   | 03/01/2021   | No                     | No                     |
+----+------------+--------------+------------------------+------------------------+
| 1  | John Doe   | 05/01/2021   | Yes                    | No                     |
+----+------------+--------------+------------------------+------------------------+
| 1  | John Doe   | 06/01/2021   | Yes                    | No                     |
+----+------------+--------------+------------------------+------------------------+
| 2  | Sam        | 05/01/2021   | No                     | Yes                    |
+----+------------+--------------+------------------------+------------------------+

由于 Sam 的风险级别从“中”变为“低”,即降低,Decrease_In_Risk_Level 标志更新为 'Yes'

使用window函数lag() over()

您可能会注意到 CASE 决定了 Increase/Decrease。理想情况下,您会有一个数字风险评级,但这适用于当前数据集。

例子

Declare @YourTable Table ([ID] int,[MemberName] varchar(50),[Risk level] varchar(50),[Discharge Date] date)
Insert Into @YourTable Values 
 (1,'John Doe','Low','03/05/2021')
,(1,'John Doe','Medium','05/10/2021')
,(1,'John Doe','High','06/10/2021')
,(2,'Sam','Medium','05/10/2021')
,(2,'Sam','Low','05/20/2021')

;with cte as (
      Select * 
            ,RiskLag = lag([Risk Level],1,[Risk Level]) over (partition by id order by [Discharge Date])
            ,RN = row_number() over (partition by id,year([Discharge Date]),month([Discharge Date]) order by [Discharge Date] desc)
       From  @YourTable
)
Select ID
      ,MemberName
      ,StartOfMonth  = convert(date,dateadd(MONTH,datediff(MONTH,0,[Discharge Date]),0))
      ,Increase_Risk = case when right(RiskLag,1)>right([Risk level],1) 
                            then 'Yes'
                            else 'No'
                       end
      ,Derease_Risk = case when right(RiskLag,1)<right([Risk level],1) 
                            then 'Yes'
                            else 'No'
                       end

 From  cte
 Where RN=1

结果

ID  MemberName  StartOfMonth    Increase_Risk   Derease_Risk
1   John Doe    2021-03-01      No              No
1   John Doe    2021-05-01      Yes             No
1   John Doe    2021-06-01      Yes             No
2   Sam         2021-05-01      No              Yes

我创建了一个名为 riskScore 的虚拟列,它分配了以下分数: 低 = 1,中 = 2,高 = 3

此列用作与前几个月的比较。我使用 LAG() 收集前几个月的风险评分。一旦我有了前几个月的价值,我就会做一个案例陈述来比较风险评分与 prior_risk,如下所示:

WITH base as (
SELECT 
    *, 
    CASE WHEN risklevel = 'Low' THEN 1 
    WHEN risklevel = 'Medium' THEN 2 
    WHEN risklevel = 'High' THEN 3 END AS RiskScore 
FROM patient), 

score as (
SELECT 
   *, 
   LAG(riskscore) OVER (PARTITION BY id ORDER BY DischargeDt ASC) 
   AS prior_risk 
FROM base)

SELECT 
   *, 
  CASE WHEN prior_risk is NULL THEN NULL 
  WHEN riskscore > prior_risk THEN 'Increased' 
  ELSE 'Decreased' END AS status 
FROM score

注意:您的月初逻辑是正确的,如果需要,您可以将其添加到提供的逻辑中。此外,riskScore 和 prior_score 列用于演示目的,您可以将其从最终输出中删除。