Re: [SQL ] MSSQL字段自动取得上一笔的字段

楼主: cutekid (可爱小孩子)   2018-02-13 16:47:51
假设你 join 数张 table 得到的结果叫: result
;with tb1 as (
select t1.*,
case when t1.COLA = 'GOOD' then t1.ID else t2.ID end as COLB,
row_number() over (partition by t1.ID order by t2.ID desc) as num
from result t1 left join result t2
on t1.COLA != 'GOOD' and t2.COLA = 'GOOD' and t1.ID > t2.ID
)
select *
from tb1
where num = 1
※ 引述《Nowtreel (盗版陈金城)》之铭言:
: 数据库-MSSQL-2012
: 各位好
: 数据库新手想请教
: 目前组出一张表,有字段ID ... COLA
: 是JOIN数张TABLE得到的结果
: 如下:
: ID ... COLA COLB
:
作者: Nowtreel (盗版陈金城)   2018-02-16 03:09:00
看来能达到我想要的效果,感谢您

Links booklink

Contact Us: admin [ a t ] ucptt.com