日期:2014-05-18 浏览次数:20905 次
create table table1
(field1 char(4), field2 char(4), field3 char(4))
insert into table1
select 'A1', 'A2', null union all
select null, 'B2', 'B3' union all
select 'C1', null, null
;with t4 as
(select row_number() over(partition by rn order by f desc) fn, t3.rn,t3.f,t3.c
from
(select rn,f,c
from (select row_number() over(order by getdate()) rn, field1,field2,field3 from table1) t1
unpivot(c for f in(field1,field2,field3)) t2) t3
)
select c value,f field
from t4 where fn=1
value field
----- --------
A2 field2
B3 field3
C1 field1