2017-10-05 50 views
0

我需要使用SSRS计算两个日期之间的药物使用率变化率。我习惯于使用SQL,因此我在SSRS上遇到困难。计算SSRS中的变化率

我有以下信息:

       Avg_Alcohol_Use_Month Avg_Drug_Use_Month 
First_interview_Date    1.63%     1.34% 
     1/30/2017 

Followup_interview_date   2.58%      .80% 
     6/30/2017 

如何创建一个反映变化的药物使用两个日期之间的比率的报告?我需要在SSRS中创建报告,但是,我不知道如何在SSRS中编写能够反映变化率的查询。

我无法在SQL中创建查询,因为我只能通过SSRS访问数据。

+0

你能澄清你的问题吗?我不确定你在期待什么。 (表达式帮助,计算字段,数据集) –

+0

如果您更习惯于使用SQL,那么在数据集中完成工作可能更容易,因为这将是纯SQL。 SSRS表达式处理由数据集提供的数据,而数据集通常是从SQL Server的查询或存储过程中检索的数据。 –

回答

0

(这个例子是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的数据集。