SQL Group by sex then age -
i have patients table below:
patients
name gender dob student m 03-mar-2001 student b f 08-dec-1985 student c f 12-sep-1990 student d m 20-may-1981
i have sql statement display result below:
gender 0-19 20-29 30-39 m 1 0 1 f 0 2 0
please check below query
create table #t1 (name varchar(100),gender varchar(10),dob date) insert #t1(name,gender,dob) values('student a','m','03-mar-2001') insert #t1(name,gender,dob) values('student b','f','08-dec-1985') insert #t1(name,gender,dob) values('student c','f','12-sep-1990') insert #t1(name,gender,dob) values('student d','m','20-may-1981') select gender , sum(case when datediff(yy,dob,getdate()) between 0 , 20 1 else 0 end) [0-19], sum(case when datediff(yy,dob,getdate()) between 21 , 30 1 else 0 end) [20-29], sum(case when datediff(yy,dob,getdate()) between 31 , 40 1 else 0 end) [30-39] #t1 group gender
Comments
Post a Comment