|
|
with t (t1,t2,t3) as (
values
('A1', 1 ,'01'),('A1' ,1, '02'),('A1', 1, '03'),
('A2', 1, '01'),('A2', 2, '01'),('A2', 2 ,'02')
),
t1(t11,t22,t33,t44) as (
select t1,t2,t3,rownumber() over(partition by t1,t2) from t
),
t3(s1,s2,s3,s4) as(
select t11,t22,cast(t33 as varchar(100)),t44 from t1 where T44 =1
union all
select a.s1,a.s2,cast(a.s3||'-'||b.t33 as varchar(100)),a.s4+1 from t3 a,t1 b where a.s1=b.t11 and a.s2=b.t22 and a.s4 = b.t44-1
)
select s1,s2,s3 from t3 x where x.s4=(select max(s4) from t3 y where x.s1=y.s1 and x.s2=y.s2) order by s1,s2 ;
这样写也是可以的
不知道楼主多加一列的目的是什么?请教一下 |
|