zoukankan      html  css  js  c++  java
  • sqlserver 同一列不同数据放到一行不同列

    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;

      ②、结果

        

           ③、转换文字列

    select Name
      ,max( case when Type=1 then '初级' else null end )Type1
      ,max( case when Type=2 then '中级' else null end )Type2
      ,max( case when Type=3 then '高级' else null end )Type3
      from [WeChatApp].[dbo].[Templates] where Id in (select id from #templet)
      group by Name;

             ④、查询结果

                   

  • 相关阅读:
    django序列化器Serializers
    django中模型类变更问题
    django图书管理系统-外键字段的增删改查
    django图书管理系统模型创建
    django中使用KindEditor上传图片
    成长
    git提交代码的经验
    react项目打包
    node——moudle
    git
  • 原文地址:https://www.cnblogs.com/study10000/p/13036872.html
Copyright © 2011-2022 走看看