JOIN中的SQL SELECT在具有特定值的行之前的最后(最大)行

时间:2018-03-01 11:17:20

标签: sql select db2

我有表T1:

TICKETID
1
2
3

我有表T2

ID  TICKETID  DESCRIPTION
1   1         First
2   2         Second
3   2         Automatic

我想加入这些表(SELECT必须从T1表开始)并在Automatic值之前提取Last Description值(例如,因为自动值是TICKETID 2,它表示MAXIMUM ID之前的最后一个值(即3)这意味着应该提取TICKETID 2的值Second。 对于TICKETID 1和TICKETID,因为自动值不存在,它将具有空值。

所以最后的结果应该是

1 null
2 Second
3 null

我试过这样:

    select t11.ticketid, t21.description
    from t1 as t11
    left outer join t2 as t21 on t21.ticketid=t11.ticketid 
    left outer join
    (
    select t12.ticketid, max(t22.id) as maxwl from t2 as t22
    inner join t1 as t12 on t12.ticketid=t22.ticketid and t22.ticketid in 
    (select t23.ticketid from t2 as t23 inner join 
t1 as t13 on t13.ticketid=t23.ticketid and t23.description='Automatic')
    and t22.description!='Automatic'
    group by t12.ticketid
    ) as t24 on t22.maxwl=t21.id
然后我得到了结果:

1 First
2 Second
3 null

这是错误的,因为TICKETID 1的值必须为Null,因为没有值为Automatic的描述。

3 个答案:

答案 0 :(得分:1)

应该是这样的。您只需在加入前检查两个条件。但是,当您阅读相同的表3时,这不是最佳的

select t11.ticketid, t21.description
from t1 as t11

left outer join 
    (   select * 
        from t2 as t22
        where exists 
            (   select max(t23.id) as maxid, t23.ticketid
                from t2 as t23
                where t23.description <> 'Automatic'
                    and t22.ticketid = t23.ticketid
                            and exists 
                            (   select max(t24.id) as max_aut_id, t24.ticketid
                                from t2 as t24 
                                where t24.description = 'Automatic' 
                                    and t23.ticketid = t24.ticketid
                                group by t24.ticketid
                                having t23.id < max_aut_id)
                group by t23.ticketid
                having maxid = t22.id)
    ) as t21 
    on t21.ticketid = t11.ticketid

更新:已修复,因此它将是自动之前的最新版本,而不仅仅是最新的非自动版本。

答案 1 :(得分:0)

Ohter方法:

select t1.*, t4.description 
from t1                             
left outer join lateral                      
(                                            
   select * from t2                          
   where t1.TICKETID=t2.TICKETID             
   and t2.description='Automatic'            
   order by t2.id                            
   fetch first rows only                     
) t3 on 1=1 

left outer join lateral                      
(                                            
   select * from t2                          
   where t1.TICKETID=t2.TICKETID             
   and t2.id<t3.id                           
   and t2.description<>'Automatic'           
   order by t2.id desc                       
   fetch first rows only                     
) t4 on 1=1 

答案 2 :(得分:0)

其他方法:

with t2withrang as (                                             
select t2.*, rownumber() over(                                   
partition by t2.TICKETID ,                                       
case when description ='Automatic' then 0 else 1 end             
order by t2.id desc ) rang,                                      
ifnull((select 1 from t2 t2b where t2.id<>t2b.id                 
and t2.TICKETID = t2b.TICKETID and t2b.description='Automatic'   
fetch first rows only )                                          
, 0) hasautomatic                                                
from t2                                                          
)                                                                
select t1.*, t3.description                                      
from t1 left outer join t2withrang t3                            
on t1.TICKETID=t3.TICKETID and t3.description<>'Automatic'       
and rang=1 and  hasautomatic=1