SELECT distinct
      u.Name as UserName,
      a.OrderId,
      (
      select  d.SignCity from T_OperaterInfo o left join T_DepartmentInfo d on o.DepId=d.DepId where o.OptId=a.CreateUserId
      ) jinjianchengshi,
      o.Name as lurukefu,
      p.ProductName,
      VerifyName = v.VerifyName ,
      DepName=
      (
      select  D6.DepName from  T_OperaterInfo O5
      left join  T_DepartmentInfo D5 on D5.DepId = O5.DepId
      left join  T_DepartmentInfo D6 on D6.DepId = D5.ParentDepId
      where  a.FirstVerifyOptId = O5.OptId
      ) ,
      a.NetAmount,
      a.Amount,
      a.Term,
      a.RepayPerMonth,
      (select count(1) from T_VerifySituation v1 where v1.VerifyStatus=1005 and v1.OrderId=a.OrderId) tuihuicishu,
      vst3.RefuseWayName as  jujueyuanyin,
      vst10.VerifyDate   as  lixiangshijian,
      'jinjianshijian' =
      case
      when  ( CONVERT(varchar(100), vs2.VerifyDate, 20)) is not null then ( CONVERT(varchar(100), vs2.VerifyDate, 20))
      else
      CONVERT(varchar(100),  vst4.VerifyDate , 20)
      end,
      vst5.VerifyDate  as  zongpingshijian,
      CONVERT(varchar(100), a.ContractStartDate, 20) jihuaqianyueshijian ,
      CONVERT(varchar(100), a.ContractEndDate, 20) hetongjieshushijian,
      (select O1.Name from T_OperaterInfo O1 where O1.OptId=  a.FirstVerifyOptId ) chushen,
      (select O2.Name from T_OperaterInfo O2 where O2.OptId=  a.LastVerifyOptId ) zhongshen,
      (select O3.Name from T_OperaterInfo O3 where O3.OptId=  a.TotalReviewOptId ) zongping,
      (select O4.Name from T_OperaterInfo O4 where O4.OptId=  a.ReconsiderOptId ) fuyi,
      ''''+(  select cast(u.IDCard as varchar(25)))  as  IDCard,
      vst6.AuditOpinion as  chushenyijian,
      vst7.AuditOpinion as   zhongshenyijian,
      vst8.AuditOpinion as   zongpingyijian,
      vst9.AuditOpinion as  fuyiyijian,
      shifougenggaichanpin = case when a.IsAccordanceProduct=1 then '是' else '否' end ,
      'ChangeProductName'=
      case
      when  (a.ChangeProductName is not null and a.ChangeProductName != '')   then substring(a.ChangeProductName,0,charindex(',', ChangeProductName))
      end,
      shifoulianmingjiekuan =case when a.IsCoborrowLoan = 1 then '是' else '否' end,
      'CoborrowName'=
      case
      when  (a.CoborrowName is not null and a.CoborrowName !=  '')   then substring(a.CoborrowName,0,charindex(',', CoborrowName))
      end
      FROM  T_LoanProductOrder as a
      inner join T_UserInfo as u on a.UserId=u.UserId
      inner join T_VerfyStatus as v on a.VerifyStatus=v.VerifyId
      inner join T_Product as p on a.ProductTypeId=p.ProductId
      left join T_OperaterInfo as o on o.OptId=a.CreateUserId
      left join T_DepartmentInfo as d on o.DepId=d.DepId
      left join T_UserWorkInfo as w on u.UserId=w.UserId
      left join (select     OrderId, VerifyDate = max (VerifyDate)  ,flag=count(*) from T_VerifySituation where  VerifyStatus=3023 group by OrderId  ) vs2 on a.OrderId=vs2.OrderId
      left join (select    OrderId, RefuseWayName ,VerifyDate = max (VerifyDate) ,flag=count(*) from T_VerifySituation where  VerifyStatus in (1015,1016) group by OrderId ,RefuseWayName) vst3 on a.OrderId=vst3.OrderId
      left join (select    OrderId ,VerifyDate = max (VerifyDate)  ,flag=count(*) from T_VerifySituation where  VerifyStatus in (1002) group by OrderId ) vst4 on a.OrderId=vst4.OrderId
      left join (select    OrderId ,VerifyDate = max (VerifyDate) ,flag=count(*)  from T_VerifySituation where  VerifyStatus in (1008 ,1015 )   group by OrderId ) vst5 on a.OrderId=vst5.OrderId
      left join (select OrderId ,VerifyDate,AuditOpinion from (
select ROW_NUMBER() over(partition by OrderId order by VerifyDate desc) ROWNO,OrderId ,VerifyDate = max (VerifyDate),AuditOpinion 
from T_VerifySituation where  VerifyStatus in (1004,1005,3013,3016,1028 )  group by OrderId,VerifyDate,AuditOpinion
) as t   where ROWNO=1 ) vst6 on a.OrderId=vst6.OrderId
      left join (
 select OrderId ,VerifyDate,AuditOpinion from (
select ROW_NUMBER() over(partition by OrderId order by VerifyDate desc) ROWNO,OrderId ,VerifyDate = max (VerifyDate),AuditOpinion 
from T_VerifySituation where  VerifyStatus in (1006,1007,3018,3025,1029 )  group by OrderId,VerifyDate,AuditOpinion
) as t   where ROWNO=1 ) vst7 on a.OrderId=vst7.OrderId
      left join (
 select OrderId ,VerifyDate,AuditOpinion from (
select ROW_NUMBER() over(partition by OrderId order by VerifyDate desc) ROWNO,OrderId ,VerifyDate = max (VerifyDate),AuditOpinion 
from T_VerifySituation where  VerifyStatus in (1008, 1009 ,1015)  group by OrderId,VerifyDate,AuditOpinion
) as t   where ROWNO=1
 ) vst8 on a.OrderId=vst8.OrderId
      left join (
 select OrderId ,VerifyDate,AuditOpinion from (
select ROW_NUMBER() over(partition by OrderId order by VerifyDate desc) ROWNO,OrderId ,VerifyDate = max (VerifyDate),AuditOpinion 
from T_VerifySituation where  VerifyStatus in (1010,1011)  group by OrderId,VerifyDate,AuditOpinion
) as t  where ROWNO=1 ) vst9 on a.OrderId=vst9.OrderId
      left join (select    OrderId ,VerifyDate = min (VerifyDate),AuditOpinion ,flag=count(*)  from T_VerifySituation where  VerifyStatus in (1002) group by OrderId ,AuditOpinion ) vst10 on a.OrderId=vst10.OrderId
      where 1=1
  and a.ProductTypeId !=30013 
  and a.OrderId=11376

复杂的sql 一对多查询的更多相关文章

  1. sql 一对多查询

    1. 一对多查询 查询departmentinfo字典下所有部门的人员数量 select * from departmentinfo a left join (select count(*) User ...

  2. sql 一对多查询最近一条

    感谢 http://bbs.csdn.net/topics/391048578?page=1 create table A ( [Id] [uniqueidentifier] NOT NULL, ) ...

  3. [译]LINT TO SQL 介绍(数据库查询) - Part.3

    出处:Linq To Sql (Part.3 – Querying our database) 术语表 Built-in:内置的 Clause:子句 Debugger:调试器 Object Relat ...

  4. mybatis0204 一对多查询

    查询所有订单信息及订单下的订单明细信息. sql语句 主查询表:订单表 关联查询表:订单明细 SELECT orders.*, user.username, user.sex , orderdetai ...

  5. JAVAEE——Mybatis第二天:输入和输出映射、动态sql、关联查询、Mybatis整合spring、Mybatis逆向工程

    1. 学习计划 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If标签 b) Where标签 c) Sql片段 d) Foreach标签 3.关联查询 a) 一对 ...

  6. mybatis的一对一,一对多查询,延迟加载,缓存介绍

    一对一查询 需求 查询订单信息关联查询用户信息 sql语句 /*通过orders关联查询用户使用user_id一个外键,只能关联查询出一条用户记录就可以使用内连接*/ SELECT orders.*, ...

  7. mybatis第二天——动态SQL与关联查询

    大纲摘要: 1.输入映射和输出映射 a) 输入参数映射 b) 返回值映射 2.动态sql a) If b) Where c) Foreach d) Sql片段 3.关联查询 a) 一对一关联 b) 一 ...

  8. mybatis由浅入深day02_3一对多查询

    3 一对多查询 3.1 需求(查询订单及订单明细的信息) 查询订单及订单明细的信息. 3.2 sql语句 确定主查询表:订单表 确定关联查询表:订单明细表 在一对一查询基础上添加订单明细表关联即可. ...

  9. hibernate一对多查询

    一对多查询 1,同时添加老师和学生案例 在进行具有关联关系的对象同时添加时 首先绑定对像间的关系 ---将多方关联一方 ---将一方关联多方 然后全部添加 备注: 1,保存老师对象时, 由于设置了学生 ...

随机推荐

  1. NeHe OpenGL教程 第十一课:飘动的旗帜

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  2. memcache 内部原理实现

    Lazy Expiration memcached 内部不会监视记录是否过期,而是在 get 时查看记录的时间戳,检查记录是否过期.这 种技术被称为 lazy(惰性)expiration.因此,mem ...

  3. 【JavaScript】日期和数字格式化

    var date, number; /** * 让日期和时间按照指定的格式显示的方法 * @param date * @param formatString format 格式字符串 * @retur ...

  4. (转)linux中fork()函数详解

    一.fork入门知识 一个进程,包括代码.数据和分配给进程的资源.fork()函数通过系统调用创建一个与原来进程几乎完全相同的进程,也就是两个进程可以做完全相同的事,但如果初始参数或者传入的变量不同, ...

  5. memcpy

    函数原型 void *memcpy(void*dest, const void *src, size_t n); 功能 由src指向地址为起始地址的连续n个字节的数据复制到以destin指向地址为起始 ...

  6. struts2标签获取parameter,request,session,application中的值

    http://localhost:8080/demo/index.jsp?flag=kkkk <s:property value="#parameters.flag" /&g ...

  7. Flask-SQLAlchemy 学习总结

    初始化和配置 ORM(Object Relational Mapper) 对象关系映射.指将面对对象得方法映射到数据库中的关系对象中.Flask-SQLAlchemy是一个Flask扩展,能够支持多种 ...

  8. Need help with design ReadOnlyListBase (Insert, Update, Delete from ReadOnlyListBase)

    原文地址:http://forums.lhotka.net/forums/p/3166/21214.aspx My task is: For select client, I have a modal ...

  9. Configure apt-get / git/ curl to use a proxy (Ubuntu)

    http://technoblog.org/2009/07/configure-apt-get-to-use-a-proxy-ubuntu/ Open the following configurat ...

  10. mysql 使用说明-1

    以下内容是根据官方文档翻译的,执行截图是博主自己实测结果. 3.1 Connecting to and Disconnecting from the Server 连接,断开数据库 要连接到mysql ...