;with t1 as (select row_number() over (partition by 条码,跟踪单 order by 测量总长 ) as rn,* from 品保 where 量测时间>'2022-09-20') ,t2 as (select 条码,跟踪单, case rn when 1 then 测量总长 else null end as 长度1, case rn when 2 then 测量总长 else null end as 长度2, case rn when 3 then 测量总长 else null end as 长度3, case rn when 4 then 测量总长 else null end as 长度4, case rn when 5 then 测量总长 else null end as 长度5, case rn when 6 then 测量总长 else null end as 长度6, case rn when 7 then 测量总长 else null end as 长度7, case rn when 8 then 测量总长 else null end as 长度8 from t1) select 条码,跟踪单,max(长度1) as 长度1, max(长度2) as 长度2, max(长度3) as 长度3, max(长度4) as 长度4, max(长度5) as 长度5, max(长度6) as 长度6, max(长度7) as 长度7, max(长度8) as 长度8 from t2 group by 条码,跟踪单
标签:case,max,when,转列,固定,长度,rn,null From: https://www.cnblogs.com/jjyy2008cn/p/16724885.html