SSRS创建年龄范围

时间:2016-08-16 19:19:30

标签: sql-server-2008 ssrs-2008-r2

我试图制作一份报告,显示在特定时间范围内有多少患者进入某个年龄段。这是我到目前为止所得到的,但输出的数字是错误的,所以我不确定我错过了什么。我在这里跟了几个例子,但到目前为止还没有一个例子。不知道是不是因为我加入了另一张桌子或者什么。

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)

1 个答案:

答案 0 :(得分: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)&gt; = @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...