Proc SQL; connect to teradata (database=); create table here.new as select * from connection to teradata (SELECT d.Nbr, d.Sequence, a.Nb, '-' as Duration, (Case When a.Dir = b.Dir Then 'S' Else 'M' END) as Type, AL.Gphc_Adrress, AL4.Gphc_Adr2, FROM OP.Base a LEFT OUTER JOIN MV.STN b ON (a.Cd=b.Cd AND a.Cd=b.Cd) FROM OP.D5a, MV.D5b WHERE (Doc=Doc AND D5.Ldt=LDt AND D5.Sq=Sq) AND ((D5a.Inav='N' AND D5a.Ind='N' AND (D5b.Cd='D' OR D5b.Op='D') LEFT OUTER JOIN ( SELECT DISTINCT D.kt, D.LDt, D.Sq, SUM ( D6.Dc ) FROM OPMV.CHRG WHERE (D6.LDt BETWEEN DATE '2012-07-26' AND DATE '2012-07-31') GROUP BY D6.Doc_Nb, D6.Doc_LDt, D6.Sq) AL(nbr, dt, sq, amt) ON (AL.nbr=AL.nbr LEFT OUTER JOIN ( SELECT D7.Doc_Nb, SUM ( D7b.Dc ) FROM OPMV.TK LEFT OUTER JOIN OPMV.MISC ON (D7a.Cur_RM=D7b.Rm_Nb WHERE ((D7a.LDt BETWEEN DATE '2012-07-26' AND DATE '2012-07-31' GROUP BY D7a.Doc_Nb, D7a.Iss_LDt, ORDER BY 4,1,3 ); quit;