表1  
DeviceNO                            DeviceName                        CatalogNO  
---------------------  -----------------------------------------------------------------------  
B0001                                  笔记本                                      10001  
D0001                                  打印机                                      10001  
T0001                                  台式机                                      10001  
T0002                                  投影机                                      10001  
 
表2  
DeviceNO                            ApplyUseID  ApplyReturnID  
---------------------  ----------  -------------  
B0001                                  1001              1003              
T0001                                  1002              NULL  
 
表3  
ApplyID            ApplyUserName      ApplyDatetime                    ApplyFlag  
----------------------  -----------------------  -----------  -----------  
1001                张三                    2004-05-15  00:00:00.000              0  
1002                  李四                  2006-01-01  00:00:00.000                0  
1003                  张三                  2004-06-16  00:00:00.000                1  
 
 
结果如下图所示(注意,当前状态是根据    表2中  ApplyReturnID字段是否为空作判断的)  
 
 资产编号  资产名称    领用时间       归还时间      使用者        当前状态    
 B0001      笔记本    2004-05-15     2004-06-16      张三          已归还 
 T0001     台式机    2006-01-01      null      李四             使用中 
 

解决方案 »

  1.   

    select
        a.DeviceNO      as 资产编号,
        a.DeviceName    as 资产名称,
        c.ApplyDatetime as 领用时间,
        d.ApplyDatetime as 归还时间,
        c.ApplyUserName as 使用者  ,
        (case when b.ApplyReturnID is null then '使用中' else '已归还' end) as 当前状态  
    from
        表1 a
    inner join
        表2 b
    on
        a.DeviceNO=b.DeviceNO
    inner join
        表3 c
    on
        b.ApplyUseID=c.ApplyUseID
    left join
        表3 d
    on
        b.ApplyReturnID=d.ApplyUseID
      

  2.   

    create table t1
    (DeviceNO varchar(15),
     DeviceName varchar(15),
     CatalogNO varchar(15)
    )
    insert into t1 select 'B0001','笔记本','10001'
    union all select 'D0001','打印机','10001'
    union all select 'T0001','台式机','10001'
    union all select 'T0002','投影机','10001'
    gocreate table t2
    (DeviceNO varchar(15),
     ApplyUseID varchar(15),
     ApplyReturnID varchar(15)
    )
    insert into t2 select 'B0001','1001','1003'
    union all select 'T0001','1002',null
    gocreate table t3
    (ApplyID varchar(15),
     ApplyUserName varchar(15),
     ApplyDatetime varchar(15),
     ApplyFlag int
    )
    insert into t3 select '1001','张三','2004-05-15',0
    union all select '1002','李四','2006-01-01',0
    union all select '1003','张三','2004-06-16',1
    goselect a.deviceno 资产编号,
    t1.devicename 资产名称,
    a.applydatetime 领用时间,
    t3.applydatetime 归还时间,
    t3.applyusername 使用者,
    case when applyreturnid is not null then '归还' else '使用中' end as '当前状态'
    from
    (select deviceno,applydatetime,applyreturnid 
    from t2 left join t3 on t2.applyuseid=t3.applyid) a 
    left join t3 on a.applyreturnid=t3.applyid 
    inner join t1 on a.deviceno=t1.deviceno资产编号            资产名称            领用时间            归还时间            使用者             当前状态   
    --------------- --------------- --------------- --------------- --------------- ------ 
    B0001           笔记本             2004-05-15      2004-06-16      张三              归还
    T0001           台式机             2006-01-01      NULL            NULL            使用中(所影响的行数为 2 行)
      

  3.   

    select a.deviceno 资产编号,
    t1.devicename 资产名称,
    a.applydatetime 领用时间,
    t3.applydatetime 归还时间,
    t3.applyusername 使用者,
    case when applyreturnid is not null then '归还' else '使用中' end as '当前状态'
    from
    (select deviceno,applydatetime,applyuseid,applyreturnid 
    from t2 left join t3 on t2.applyuseid=t3.applyid) a 
    left join t3 on a.applyuseid=t3.applyid 
    inner join t1 on a.deviceno=t1.deviceno资产编号            资产名称            领用时间            归还时间            使用者             当前状态   
    --------------- --------------- --------------- --------------- --------------- ------ 
    B0001           笔记本             2004-05-15      2004-05-15      张三              归还
    T0001           台式机             2006-01-01      2006-01-01      李四              使用中(所影响的行数为 2 行)
      

  4.   

    --再一次
    select a.deviceno 资产编号,
    t1.devicename 资产名称,
    a.applydatetime 领用时间,
    t3.applydatetime 归还时间,
    a.applyusername 使用者,
    case when applyreturnid is not null then '归还' else '使用中' end as '当前状态'
    from
    (select deviceno,applydatetime,applyuseid,applyreturnid,applyusername 
    from t2 left join t3 on t2.applyuseid=t3.applyid) a 
    left join t3 on a.applyreturnid=t3.applyid 
    inner join t1 on a.deviceno=t1.deviceno资产编号        资产名称        领用时间        归还时间        使用者    当前状态   
    --------------- --------------- --------------- --------------- ------------- ------ 
    B0001           笔记本             2004-05-15      2004-06-16      张三              归还
    T0001           台式机             2006-01-01      NULL            李四              使用中(所影响的行数为 2 行)