预期结果数据: 如何计算在添加和停止剂量时向客户提供的时间范围内的剂量总数。在某些情况下,可能会创建新的开始日期和结束日期。如 patid=147 的解释
逻辑是,例如,如果患者在 01/01/2024 至 01/07/2024 期间接受 25 毫克剂量,并且从 01/04/20204 -01/17/2024 期间接受额外的 30 毫克剂量。这意味着新指定日期的准确计数将为 01/01/2024-01/03/2024=25 mg 01/04/2024 -01/07/2024 = 55mg (25+30) mg,然后从 01/08 /2024-01/17/2024 =30毫克,因为客户已停止服用25毫克剂量。
drop table #prescribed_dose
drop table #prescribed_dose_Result
CREATE TABLE #prescribed_dose (
[patid] [varchar](20) NULL
,[Generic_Name] [varchar](256) NULL
,[Route] [varchar](50) NULL
,[s_date] DATE
,[e_date] DATE
,[Ordered_Dose] [numeric](15, 2) NULL
,[cadance_value] int--if not null
--,[ext_date] DATE--end date plus candance value
)
INSERT INTO #prescribed_dose
VALUES
('125','Haloperidol Decanoate','R','2016-07-21','2016-07-25',100.00,14)
,('125','Paliperidone Palmitate','O','2016-07-21','2016-07-22',234.00,null)
,('125','risperiDONE Consta','R','2016-01-27','2016-03-01',12.50,14)
,('125','risperiDONE Consta','0','2016-02-03' ,'2016-02-04',25.00,null)
,('125','risperiDONE Consta','R','2016-02-17','2016-03-01' ,25.00,14)
,('125','risperiDONE Consta','R','2016-07-27','2016-08-09',25.00,null)
,('141','Haloperidol Decanoate','R','2016-05-14','2016-07-26',250.00,30)
,('141','Haloperidol Decanoate','R','2016-08-05','2016-08-10',100.00,14)
,('141','Paliperidone Palmitate','O','2016-08-05','2016-08-06',234.00,null)
,('141','risperiDONE Consta','R','2016-05-25','2016-07-26' ,50.00,14)
,('141','risperiDONE Consta','R','2016-07-27','2016-08-10',25.00,21)
,('147','Paliperidone Palmitate','R','2019-10-03','2020-10-02',234.00,30)
,('147','risperiDONE Consta','R','2019-10-15','2019-10-15',12.50,14)
,('147','risperiDONE Consta','R','2019-10-15' ,'2020-10-14',12.50,14)
,('147','risperiDONE Consta','R','2019-10-15','2020-10-14' ,12.50,14)
,('147','risperiDONE Consta','R','2019-11-04','2020-11-03',25.00,14)
------desired result
CREATE TABLE #prescribed_dose_Result (
[patid] [varchar](20) NULL
,[Generic_Name] [varchar](256) NULL
,[Route] [varchar](50) NULL
,[s_date] DATE
,[e_date] DATE
,[Ordered_Dose] [numeric](15, 2) NULL----Total Dose - if multiple orders for same drug overlap, add them together
,[cadance_value] int--if not null
--,[ext_date] DATE--end date plus candance value
)
INSERT INTO #prescribed_dose_Result
VALUES
('125','Haloperidol Decanoate','R','2016-07-21','2016-07-25',100.00,14)
,('125','Paliperidone Palmitate','O','2016-07-21','2016-07-22',234.00,null)
,('125','risperiDONE Consta','R','2016-01-27','2016-02-16',12.50,14)
,('125','risperiDONE Consta','R','2016-02-17' ,'2016-03-01',37.50,null)
----- 37.50 includes the order for 12.5 MG that begins on 1/27 and ends on 3/1
--and the order for 25 MG that begins on 2/17 and ends on 3/1.
,('125','risperiDONE Consta','R','2016-07-27','2016-08-09' ,25.00,14)
,('125','risperiDONE Consta','O','2016-02-03','2016-02-04',25.00,null)
,('141','Haloperidol Decanoate','R','2016-05-14','2016-07-26',250.00,30)
,('141','Haloperidol Decanoate','R','2016-08-05','2016-08-10',100.00,14)
,('141','Paliperidone Palmitate','O','2016-08-05','2016-08-06',234.00,null)
,('141','risperiDONE Consta','R','2016-05-25','2016-07-26' ,50.00,14)
,('141','risperiDONE Consta','R','2016-07-27','2016-08-10',25.00,21)
,('147','Paliperidone Palmitate','R','2019-10-03','2020-10-02',234.00,30)
,('147','risperiDONE Consta','R','2019-10-15','2019-10-15',37.50,14)
---There are 3 orders for 12.5 MG dose active on 10/15.
--One is only for 10/15 the other two continue until 10/14/2020.
--Another order for 25 mg begins on 11/4/2019 and continues until 11/3/2020.
,('147','risperiDONE Consta','R','2019-10-16' ,'2019-11-03',25.00,14)
,('147','risperiDONE Consta','R','2019-11-04','2020-10-14' ,50.00,14)
,('147','risperiDONE Consta','R','2020-10-15','2020-11-03',25.00,14)
select * from #prescribed_dose
order by patid,Generic_Name,S_Date,Route
select * from #prescribed_dose_Result
order by patid,Generic_Name,S_Date,Route
您提出了一个像样的问题(这在这里很难),提供了有用的代码,但没有提供具有正确值的预期结果集。我尝试根据您的信息构建一些东西。首先,由数据中的所有日期构建日历表(递归 cte),然后将数据连接到日历表并聚合。我希望这有帮助。
DROP TABLE IF EXISTS #prescribed_dose
CREATE TABLE #prescribed_dose (
[patid] [varchar](20) NULL
,[Generic_Name] [varchar](256) NULL
,[Route] [varchar](50) NULL
,[s_date] DATE
,[e_date] DATE
,[Ordered_Dose] [numeric](15, 2) NULL
,[cadance_value] int--if not null
)
DROP TABLE IF EXISTS #prescribed_dose_Result
CREATE TABLE #prescribed_dose_Result (
[patid] [varchar](20) NULL
,[Generic_Name] [varchar](256) NULL
,[Route] [varchar](50) NULL
,[s_date] DATE
,[e_date] DATE
,[Ordered_Dose] [numeric](15, 2) NULL
,[cadance_value] int
)
INSERT INTO #prescribed_dose
VALUES
('125','Haloperidol Decanoate','R','2016-07-21','2016-07-25',100.00,14)
,('125','Paliperidone Palmitate','O','2016-07-21','2016-07-22',234.00,null)
,('125','risperiDONE Consta','R','2016-01-27','2016-03-01',12.50,14)
,('125','risperiDONE Consta','0','2016-02-03' ,'2016-02-04',25.00,null)
,('125','risperiDONE Consta','R','2016-02-17','2016-03-01' ,25.00,14)
,('125','risperiDONE Consta','R','2016-02-19','2016-02-20' , 1.00,14)
,('125','risperiDONE Consta','R','2016-07-27','2016-08-09',25.00,null)
,('141','Haloperidol Decanoate','R','2016-05-14','2016-07-26',250.00,30)
,('141','Haloperidol Decanoate','R','2016-08-05','2016-08-10',100.00,14)
,('141','Paliperidone Palmitate','O','2016-08-05','2016-08-06',234.00,null)
,('141','risperiDONE Consta','R','2016-05-25','2016-07-26' ,50.00,14)
,('141','risperiDONE Consta','R','2016-07-27','2016-08-10',25.00,21)
,('147','Paliperidone Palmitate','R','2019-10-03','2020-10-02',234.00,30)
,('147','risperiDONE Consta','R','2019-10-15','2019-10-15',12.50,14)
,('147','risperiDONE Consta','R','2019-10-15' ,'2020-10-14',12.50,14)
,('147','risperiDONE Consta','R','2019-10-15','2020-10-14' ,12.50,14)
,('147','risperiDONE Consta','R','2019-11-04','2020-11-03',25.00,14)
;WITH mima AS(
SELECT
minn = MIN(s_date)
,maxx = MAX(e_date)
FROM #prescribed_dose
)
,r AS(
SELECT
n=1
,dt = minn
FROM mima
UNION ALL
SELECT
n = n + 1
,dt = DATEADD(DAY, 1, r.dt)
FROM r
WHERE r.dt < (SELECT maxx FROM mima)
)
--SELECT
-- dt
-- ,[patid]
-- ,[Generic_Name]
-- ,[Route]
-- ,[s_date]
-- ,[e_date]
-- ,[Ordered_Dose]
-- ,[cadance_value]
--FROM r
--JOIN #prescribed_dose d
-- ON r.dt >= d.s_date
-- AND r.dt <= d.e_date
--ORDER BY
-- dt
-- ,patid
-- ,s_date
INSERT INTO #prescribed_dose_Result(
[patid]
,[Generic_Name]
,[Route]
,[s_date]
,[e_date]
,[Ordered_Dose]
,[cadance_value]
)
SELECT
[patid]
,[Generic_Name]
,[Route]
,[s_date] = MIN(dt)
,[e_date] = MAX(dt)
,[Ordered_Dose] = SUM([Ordered_Dose])
,[cadance_value]
FROM r
JOIN #prescribed_dose d
ON r.dt >= d.s_date
AND r.dt <= d.e_date
GROUP BY
[patid]
,[Generic_Name]
,[Route]
,[cadance_value]
ORDER BY
patid
,Generic_Name
,s_date
OPTION(MAXRECURSION 0)
SELECT * FROM #prescribed_dose
ORDER BY patid,Generic_Name,S_Date,Route
SELECT * FROM #prescribed_dose_Result
ORDER BY patid,Generic_Name,S_Date,Route