2016-08-16 112 views
0

我正在尝试制作一份报告,显示在特定时间范围内有多少患者进入了一个年龄段。这是我到目前为止,但其输出数字是错误的,所以我不知道我错过了什么。我已经在这里跟上了几个例子,但是迄今为止没有任何例子。不知道是因为我加入到不同的表或什么。SSRS创建年龄范围

select COUNT (DISTINCT MPFILE.PATIENT_NO) as 'Male 0-4' 
from ENHFILE 
Join MPFILE 
on MPFILE.PATIENT_NO = ENHFILE.PATIENT_NO 
where ENHFILE.COSITE = '300' 
and ENHFILE.VISIT_PURPOSE = '2' 
and MPFILE.SEX = 'M' 
and (DATEDIFF(hour,MPFILE.DOB,GETDATE())/8766) > 5 
and ENHFILE.ENCOUNTER_DATE between (@StartDate) and (@EndDate) 



select COUNT (DISTINCT MPFILE.PATIENT_NO) as 'FeMale 0-4' 
from ENHFILE 
Join MPFILE 
on MPFILE.PATIENT_NO = ENHFILE.PATIENT_NO 
where ENHFILE.COSITE = '300' 
and ENHFILE.VISIT_PURPOSE = '2' 
and MPFILE.SEX = 'F' 
and (DATEDIFF(hour,MPFILE.DOB,GETDATE())/8766) > 5 
and ENHFILE.ENCOUNTER_DATE between (@StartDate) and (@EndDate) 
+0

你想找什么样的范围?年龄在###和###之间的人们? – scsimon

+0

即时寻找0-4,5-9,10-17,18+,我认为我的错误是我在面对错误的方向在这个例子中......我现在似乎得到了正确的数字。那感觉... – mol

+0

8760是一年中的小时数,8784是闰年。 8766从哪里来?你的代码是否会占闰年? – scsimon

回答

0

这是应该得到你想要的东西。

--First i just created some test data 
if object_id('tempdb..#temp') is not null drop table #temp 

select '8/15/1995' as DOB into #temp union all --Note this person is 21 
select '8/16/1995' union all      --Note this person is 21 TODAY 
select '8/17/1995' union all      --Note this person is 21 TOMORROW 
select '4/11/1996' union all 
select '5/15/1997' union all 
select '9/7/2001' 

--set the years old you want to use here. Create another variable if you need to use ranges 
declare @yearsOld int 
set @yearsOld = 21 

select 
    convert(date,DOB) as DOB, 

    --This figures out how old they are by seeing if they have had a birthday 
    --this year and calculating accordingly. It is what is used in the filter 
    --I only put it here so you can see the results 
    CASE 
     WHEN CONVERT(DATE, CONVERT(VARCHAR(4), YEAR(GetDate()))+ '-'+ CONVERT(VARCHAR(2),MONTH(DOB)) + '-' + CONVERT(VARCHAR(2),DAY(DOB))) <= GETDATE() THEN DATEDIFF(yy,DOB,GETDATE()) 
     ELSE DATEDIFF(yy,DOB,GETDATE()) -1 
    END AS YearsOld 
from #temp 
where 
    --here is your filter. Feel free to change the >= to what ever you want, or combine it to make it a range. 
    CASE 
     WHEN CONVERT(DATE, CONVERT(VARCHAR(4), YEAR(GetDate()))+ '-'+ CONVERT(VARCHAR(2),MONTH(DOB)) + '-' + CONVERT(VARCHAR(2),DAY(DOB))) <= GETDATE() THEN DATEDIFF(yy,DOB,GETDATE()) 
     ELSE DATEDIFF(yy,DOB,GETDATE()) -1 
    END >= @yearsOld 

编辑

这是你的方法,该方法没有考虑到,如果他们今年过了一个生日。我使用一些测试数据。注意出生于1995年8月18日的人。他们把明天21,但使用(DATEDIFF(小时,DOB,GETDATE())/ 8766)> = @yearsOld当它不应该包括他们...

--First i just created some test data 
if object_id('tempdb..#temp') is not null drop table #temp 

select '8/15/1995' as DOB into #temp union all --Note this person is 21 
select '8/16/1995' union all      --Note this person is 21 TODAY 
select '8/18/1995' union all      --Note this person is 21 TOMORROW 
select '4/11/1996' union all 
select '5/15/1997' union all 
select '9/7/2001' 

--set the years old you want to use here. Create another variable if you need to use ranges 
declare @yearsOld int 
set @yearsOld = 21 

select 
    convert(date,DOB) as DOB, 

    --This figures out how old they are by seeing if they have had a birthday 
    --this year and calculating accordingly. It is what is used in the filter 
    --I only put it here so you can see the results 
    CASE 
     WHEN CONVERT(DATE, CONVERT(VARCHAR(4), YEAR(GetDate()))+ '-'+ CONVERT(VARCHAR(2),MONTH(DOB)) + '-' + CONVERT(VARCHAR(2),DAY(DOB))) <= GETDATE() THEN DATEDIFF(yy,DOB,GETDATE()) 
     ELSE DATEDIFF(yy,DOB,GETDATE()) -1 
    END AS YearsOld 
from #temp 
where 
    --here is your filter. Feel free to change the >= to what ever you want, or combine it to make it a range. 
    (DATEDIFF(hour,DOB,GETDATE())/8766) >= @yearsOld 

成绩

DOB  | YearsOld 
1995-08-15 | 21 
1995-08-16 | 21 
1995-08-18 | 20   --this shouldn't be here... 
相关问题