Saturday, 8 October 2011

Join in SQL2


data ae;
input pid aetype $ ;
cards;
101 headache
150 hairloss
190 headache
;
data adr;
input pid adrtype $;
cards;
101 eyedis
178 eyedis
190 eardis
;
proc sql;
select * from ae natural join adr;
quit;
proc sql;
select * from adr natural join ae;
quit;
/*self join*/
data emp_sale;
input eid jsale fsale;
cards;
101 340 450
102 340 200
103 320 453
;
proc sql ;
select * from emp_sale where fsale>jsale;
quit;
/*union join*/
proc sql;
select * from ae union join adr;
quit;
/* cross join*/
proc sql;
select * from ae,adr;
quit;
proc sql ;
select * from ae crosss join adr ;
quit ;
data cust_house;
input acno hm_ln $ year amount;
cards;
110010 lh023 2006 500000
110011 lh024 2006 700000
110012 lh045 2006 250000
110013 lh047 2007 300000
110014 lh057 2007 500000
;
data cust_vech;
input acno vc_ln $ year amount;
cards;
110023 lv001 2006 450000
110126 lv002 2006 300000
110012 lv012 2007 400000
110278 lvo45 2007 300000
110014 lv046 2007 400000
110345 lvo56 2007 300000
;
proc sql;
select * from cust_house where acno not in (select acno from cust_vech);
quit;
proc sql;
select * from cust_house where acno not in (select acno from cust_vech)
outer union
select * from cust_vech where accno not in(select acno from cust_house);
quit;
proc sql;
select * from cust_house where acno  in (select acno from cust_vech);
quit;

No comments:

Post a Comment