I have inherited SQL code that I know works, but being a novice SQL person, cannot figure out how it is working.
I can do the basic create, select, from, where, group by, order by code. The additonal FROMs and SELECTS have me thrown for a loop. Is it poorly written or can someone breakdown the code in english and tell me what it is doing and where it is doing it from?
Thanks for any and all help.
proc sql;
create table GROUP.REP3 as
select distinct b.reqstno label='Request Number',
b.agecat label='Age Group',
male label='Male' format=comma11.,
female label='Female' format=comma11.
from (select distinct b.reqstno, b.agecat, male
from (select distinct reqstno, agecat, sum(months99)/12 as male
from group.membs
where sex='M'
group by reqstno, agecat) a
RIGHT JOIN pat b
on a.reqstno=b.reqstno and
a.agecat=b.agecat) a
FULL JOIN
(select distinct b.reqstno, b.agecat, female
from (select distinct reqstno, agecat, sum(months99)/12 as female
from group.membs
where sex='F'
group by reqstno, agecat) a
RIGHT JOIN pat b
on a.reqstno=b.reqstno and
a.agecat=b.agecat) b
on a.reqstno=b.reqstno and
a.agecat=b.agecat;
quit;
I can do the basic create, select, from, where, group by, order by code. The additonal FROMs and SELECTS have me thrown for a loop. Is it poorly written or can someone breakdown the code in english and tell me what it is doing and where it is doing it from?
Thanks for any and all help.
proc sql;
create table GROUP.REP3 as
select distinct b.reqstno label='Request Number',
b.agecat label='Age Group',
male label='Male' format=comma11.,
female label='Female' format=comma11.
from (select distinct b.reqstno, b.agecat, male
from (select distinct reqstno, agecat, sum(months99)/12 as male
from group.membs
where sex='M'
group by reqstno, agecat) a
RIGHT JOIN pat b
on a.reqstno=b.reqstno and
a.agecat=b.agecat) a
FULL JOIN
(select distinct b.reqstno, b.agecat, female
from (select distinct reqstno, agecat, sum(months99)/12 as female
from group.membs
where sex='F'
group by reqstno, agecat) a
RIGHT JOIN pat b
on a.reqstno=b.reqstno and
a.agecat=b.agecat) b
on a.reqstno=b.reqstno and
a.agecat=b.agecat;
quit;