2016-08-16 14 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は1年の時間で、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 

EDIT

これは、彼らが今年の誕生日があった場合を考慮していない、あなたの方法です。私はいくつかのテストデータを使用します。 1995年8月18日に生まれた人に気付く。彼らは

--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... 
... 21明日を回すが、とき、それはいけない(DATEDIFF(時間、DOB、GETDATE())/ 8766)> = @yearsOldを使用すると、それらを含んでいます
関連する問題