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

SQL Server case when 日期字符串转换 多表查询 嵌套子查询的更多相关文章

  1. MS sql server 基础知识回顾(二)-表连接和子查询

    五.表连接 当数据表中存在许多重复的冗余信息时,就要考虑将这些信息建在另一张新表中,在新表中为原表设置好外键,在进行数据查询的时候,就要使用到连接了,表连接就好像两根线,线的两端分别连接两张表的不同字 ...

  2. 【网络收集】Sql Server datetime 常用日期格式转换

    ) , sfrq, ) 我们经常出于某种目的需要使用各种各样的日期格式,当然我们可以使用字符串操作来构造各种日期格式,但是有现成的函数为什么不用呢? SQL Server中文版的默认的日期字段date ...

  3. [SQL SERVER系列]之嵌套子查询和相关子查询

    子查询有两种类型,一种是只返回一个单值的子查询,这时它可以用在一个单值可以使用的地方,这时子查询可以看作是一个拥有返回值的函数:另外一种是返回一列值的子查询,这时子查询可以看作是一个在内存中临时存在的 ...

  4. SQL SERVER FOR 多列字符串连接 XML PATH 及 STUFF

    原文:SQL SERVER FOR 多列字符串连接 XML PATH 及 STUFF 本来用 Writer 写一篇关于一列多行合并的博客来的,结果快写完了时候,在一个插入代码时候,崩了,重新打开,居然 ...

  5. sql server中的日期详解使用(convert)

    转自:http://blog.csdn.net/hehe520347/article/details/48496853 有个字段值例如2012-07-02 00:00:00.000 转化成 2012- ...

  6. SQL Server 2008对日期时间类型的改进

    微软在备受多年的争议后,终于对日期时间数据类型开刀了,在新版的SQL Server 2008中一口气增加了4种新的日期时间数据类型,包括: Date:一个纯的日期数据类型. Time:一个纯的时间数据 ...

  7. sql server中的日期函数

    DATEADD   在向指定日期加上一段时间的基础上,返回新的 datetime 值. 语法           DATEADD ( datepart , number, date ) 参数 (1) ...

  8. sql server like 在将值转换成数据类型int失败

    select * from table where title like '%'?'%'; 采用? 传参会报错:sql server like 在将值转换成数据类型int失败 select * fro ...

  9. SQL Server系统函数:字符串函数

    原文:SQL Server系统函数:字符串函数 1.字符转化为ASCII,把ASCII转化为字符,注意返回的值是十进制数 select ASCII('A'),ASCII('B'),ASCII('a') ...

随机推荐

  1. nexus中央仓库中发布自己的jar包

    1.后台上传 通过nexus后台上传第三方包: http://blog.csdn.net/huchunlinnk/article/details/17789175 上面上传的只能上传release版本 ...

  2. awk语法

    awk是一个非常棒的数字处理工具.相比于sed常常作用于一整行的处理,awk则比较倾向于将一行分为数个“字段”来处理.运行效率高,而且代码简单,对格式化的文本处理能力超强.先来一个例子: 文件a,统计 ...

  3. <《巴菲特之道 (第三版)》>读书笔记

    以便宜的价格买入长期绩优的股票 他把对公司的投资看做是拥有公司的一部分股权,看重的是公司的长期经济价值 别人越是草率,我们越要加倍慎重 如果你发现自己已经在陷阱中,最重要的是想办法让自己不要再往下陷 ...

  4. 关于移动App的五个提问

    1.你的移动App利用了手机的哪些特性? 2.你们是否有用移动的角度和思维来考虑产品形态?还是简单的把Web照搬到手机上? 3.用户有什么特殊的动力去安装你们的App? 4.用户是否能很好的上手和使用 ...

  5. [emacs org-mode小技巧] org-indent-mode 让文档更容易阅读

    刚发现Emacs的org-mode里面一个叫做 org-indent-mode 的minor mode对于阅读org-mode文档很不错,版面看起来清晰多了: 从上图可以看到,org-indent-m ...

  6. hadoop的dfs工具类一个【原创】

    开始没搞定插件问题,就弄了个dsf操作类,后面搞定了插件问题,这玩意也就聊胜于无了,还是丢这里算了. 首先是一个配置,ztool.hadoop.properties hadoop.home.dir=G ...

  7. 关于织梦系统不支持php中GD库的问题

    大多数人在显成的PHP的CMS时,如织梦CMS,安装的时候不支持GD库,就导致整个网站的验证码不显示,以下是个人对此类问题的解决办法: 1.首先找到wamp的安装目录,找到PHP的文件夹,打开php. ...

  8. 泊松回归(Poisson Regression)

    本博客已经迁往http://www.kemaswill.com/, 博客园这边也会继续更新, 欢迎关注~ Linear Regression预测的目标\(Y\)是连续值, Logistic Regre ...

  9. POJ 1013 Counterfeit Dollar

    Counterfeit Dollar Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36206   Accepted: 11 ...

  10. hdinfo

    --------[ 鲁大师 ]-------------------------------------------------------------------------------- 版本: ...