(这个例子是SQL Server)
你可以做到这一点在SQL如果您保存在一个表或临时数据结构的初步结果。如果您进行子查询,您可以按照前一行的费率减去行的费率。这是按日期,因此您为给定的患者/医生选择MAX(日期),无论那个(主键?)是什么。在这种情况下,我使用“PatientID”来识别患者。见下:
--Assume your values are saved in a table or other temp table
DECLARE @tmp TABLE (PatientID int, Interview_Date date, Avg_Alcohol_Use_Month decimal (4,2), Avg_Drug_Use_Month decimal (4,2))
INSERT INTO @tmp
VALUES
(1, '2017-01-30', 1.63, 1.34)
,(2, '2017-06-30', 2.58, 0.80)
,(1, '2017-03-01', 1.54, 1.23)
,(1, '2017-07-02', 3.21, 0.20)
,(2, '2017-08-23', 2.10, 4.52)
SELECT PatientID
,Interview_Date
,Avg_Alcohol_Use_Month
,Avg_Drug_Use_Month
,Avg_Alcohol_Use_Month
-
(SELECT Avg_Alcohol_Use_Month
FROM @tmp T2
WHERE T2.PatientID = T1.PatientID
AND T2.Interview_Date = (SELECT MAX(Interview_Date)
FROM @tmp T3
WHERE T3.Interview_Date < T1.Interview_Date
AND T3.PatientID = T1.PatientID
-- or whatever PK that makes the row unique for the patient.
)
) AS [Alcohol Use Rate change]
,Avg_Drug_Use_Month
-
(SELECT Avg_Drug_Use_Month
FROM @tmp T2
WHERE T2.PatientID = T1.PatientID
AND T2.Interview_Date = (SELECT MAX(Interview_Date)
FROM @tmp T3
WHERE T3.Interview_Date < T1.Interview_Date
AND T3.PatientID = T1.PatientID
-- or whatever PK makes the row unique for the patient.
)
) AS [Drug Use Rate change]
FROM @tmp T1
ORDER BY PatientID, Interview_Date
使用这样的查询作为SSRS的数据集。
你能澄清你的问题吗?我不确定你在期待什么。 (表达式帮助,计算字段,数据集) –
如果您更习惯于使用SQL,那么在数据集中完成工作可能更容易,因为这将是纯SQL。 SSRS表达式处理由数据集提供的数据,而数据集通常是从SQL Server的查询或存储过程中检索的数据。 –