Link to home
Start Free TrialLog in
Avatar of mguptill
mguptill

asked on

Age Calculation in a sub Query

Hello I am trying to add an age calculation within a sub querry but can not seem to get it in there.
select distinct cl.uniqueid_c,cl.clientcode_c,cl.firstname_vc,cl.lastname_vc, cd.diagnosis_c, ard.description_vc,
cd.axis_c, cd.type_c,ep.type_vc as eptype, ep.status_vc,ep.episodeiden_si,
need to add a subquery here to calculate age from the birthdate_d field. any help would be great


from ar.client cl
inner join cd.clientdiagnosis cd on cl.uniqueid_c = cd.clientid_c
inner join cd.episode ep on cl.uniqueid_c = ep.clientid_c
inner join ar.diagnosiscodes dc on dc.uniqueid_c = cd.diagnosiscodeid_c
inner join ar.diagnosis ard on ard.uniqueid_c = dc.diagnosisid_c
--inner join ar.cpsplan cp on cl.uniqueid_c = cp.clientid_c
--inner join ar.psplanmaster pm on cp.psplanmasterid_c = pm.uniqueid_c
where cl.termdate_d is null and ep.enddate_d is null
--and cd.enddate_d is null
--and cl.clientcode_c not like ('ds%')
--and cd.diagnosis_c in( '299.00') and cd.type_c = 'P'
order by cl.lastname_vc
--pm.pscode_c = '0002'
--and
--cd.diagnosis_c ='294.1'
Avatar of dbeneit
dbeneit
Flag of Spain image


can you use the function datediff:

datediff(year,birthdate_d,GETDATE())
ASKER CERTIFIED SOLUTION
Avatar of dready
dready

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Scott Pletcher
SELECT ...other columns as above...,
    DATEDIFF(YEAR, birthdate_d, GETDATE()) - CASE WHEN CONVERT(CHAR(5), birthdate_d, 1) > 
        CONVERT(CHAR(5), GETDATE(), 1) THEN 1 ELSE 0 END AS [Age]
FROM ...rest same as above...
Avatar of mguptill
mguptill

ASKER

dready thanks for your help I am sorry it took so long to accept I thought I accepted your solution the day I got it.