萌新想求大佬们帮忙解答一个问题
sql吧
全部回复
仅看楼主
level 2
子目呦_ 楼主
就是如何用查询生成以下这种表呢?
求大佬们解答,谢谢谢谢
2019年09月09日 04点09分 1
level 3
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分 3
虽然萌新刚学有点看不懂,但是谢谢大佬的耐心解答[吐舌]
2019年09月09日 12点09分
大佬您还在嘛? 我想在这张表基础上加多一列数据,可以累计的那种,可以做到吗。。。
2019年09月10日 08点09分
@子目呦_ 像你之前的那样,发张你想要的结果图,我看一下
2019年09月11日 03点09分
1