level 2
create table #tmp (
type1 varchar(10),
no2 varchar(10),
year3 varchar(10))
Insert into #tmp
select 'A','01','2019' union all
select 'A','02','2019' union all
select 'B','01','2019' union all
select 'B','02','2019' union all
select 'B','03','2018' union all
select 'C','02','2020' union all
select 'C','04','2019'
select t1.type1,
t1.no2+case when t2.no2 is null then '' else ','+t2.no2 end as no2,
t1.year3
from
(select tmp.* from
(select *,ROW_NUMBER() OVER(partition by type1,year3 order by no2) RN from #tmp ) tmp
where tmp.RN=1) t1
left join
(select tmp.* from
(select *,ROW_NUMBER() OVER(partition by type1,year3 order by no2) RN from #tmp ) tmp
where tmp.RN=2 ) t2
on t1.type1=t2.type1 and t1.year3=t2.year3
order by t2.no2 desc,t1.type1,t1.no2
2019年09月09日 09点09分
