select distinct stu.*,
dbo.GetClassNameByStudentCode(stu.Code) as ClassName,dbo.GetCourseNameByStudentCode(stu.Code) CourseName,dbo.GetLinkmanByStudentId(stu.Id) Linkman,dbo.GetContactByStudentId(stu.Id) Contact,
case when svr.Linkman is not NULL then svr.Linkman else dbo.GetLinkmanByStudentId(stu.Id) end VisitLinkman,when svr.Contact then svr.Contact else dbo.GetContactByStudentId(stu.Id) VisitContact,1)">when u1.Sname NULL and u1.Sname<>'' then u1.Sname else u1.Username VisitFollowUserName,1)">CONVERT(CHAR(10),svr.FollowTime,23) VisitLastFollowTime,
scl.Name as SchoolAreaName,
svr.ContentMemo as VisitContentMemo,
dict1.Name as StudentStatusName
from FM_Student stu left join FM_StudentClass sc on stu.Code=sc.Code join FM_Class cls on cls.ClassCodesc.ClassCode join FM_Course cou on cou.Idcls.CourseId join FM_CourseSubject cs on cs.Idcou.CourseSubjectId join FM_StudentContact cc on cc.StudentIdstu.Id join FM_SchoolArea scl on scl.Idstu.SchoolAreaId join FM_StudentVisitRecord svr on svr.Id (top 1 svr1.Id FM_Student stu1 join FM_StudentVisitRecord svr1 on stu1.Code=svr1.Code and stu1.Id=stu.Id order by svr1.Id desc) join Sys_User u1 on u1.Idsvr.FollowUserID join Sys_Dict dict1 on dict1.Type='{1}' and dict1.Codestu.StudentStatus where stu.DelFlg<>{0}
*,scl.Name SchoolAreaName,dbo.GetClassNameByStudentCode(stu.Code) VisitContentMemo,dict1.Name StudentStatusName,((select sum(pf.PayAmount) FM_PayFlow pf join FM_Student stu1 on pf.CustomerCode=stu1.Code and stu1.Code=stu.Code) - ((psf.PayAmount) FM_PayStepFlow psf join FM_PayFlow pf1 on psf.PayFlowIdpf1.Id on pf1.CustomerCode=stu.Code)) as ArrearageAmount --欠费金额 0} and (sc.Status{2}or sc.Status is NULL and ((pf.PayAmount) FM_PayFlow pf > ((psf.PayAmount) FM_PayStepFlow psf pf1.Id =stu.Code)