2016-08-16 1 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 

편집

이 그들이 올해 생일을 가지고있는 경우를 고려하지 않는 방법이다. 일부 테스트 데이터를 사용합니다. 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()는가)/8,766)가> = @yearsOld 그들을 포함