1、所有数据
2、建立临时表名称、类型只显示一列
①、sql
--临时表插入筛选数据 select * into #templet from (select _hour.Id from (select id,ROW_NUMBER()OVER( partition by Name,Type order by Type desc) Rank from [WeChatApp].[dbo].[Templates])_hour where Rank=1) tab; --查询筛选后数据 select * from [WeChatApp].[dbo].[Templates] where Id in(select id from #templet)
②、结果
3、分组不同列显示同一列
①、sql
select Name ,max( case when Type=1 then 1 else 0 end )Type1 ,max( case when Type=2 then 2 else 0 end )Type2 ,max( case when Type=3 then 3 else 0 end )Type3 from [WeChatApp].[dbo].[Templates] where Id in (select id from #templet) group by Name;
②、结果