ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

sqlserver行列转换示例

2021-12-24 15:02:13  阅读:255  来源: 互联网

标签:示例 union sqlserver 员工 行列 在职 人数 select 离职


  --插入测试数据

  select '基层员工' as '范围',21826 as '在职人数',497 as '离职人数',202112 as '时间' into #T

  union all

  select '主管级及以上员工' as '范围',3904 as '在职人数',40 as '离职人数',202112 as '时间'

  union all

  select '基层员工' as '范围',21835 as '在职人数',488 as '离职人数',202101 as '时间'

  union all

  select '主管级及以上员工' as '范围',3883 as '在职人数',19 as '离职人数',202101 as '时间'

  union all

  select '基层员工' as '范围',21819 as '在职人数',472 as '离职人数',202102 as '时间'

  union all

  select '主管级及以上员工' as '范围',3900 as '在职人数',36 as '离职人数',202102 as '时间'

  union all

  select '基层员工' as '范围',22095 as '在职人数',748 as '离职人数',202103 as '时间'

  union all

  select '主管级及以上员工' as '范围',3914 as '在职人数',50 as '离职人数',202103 as '时间'

  union all

  select '基层员工' as '范围',22006 as '在职人数',659 as '离职人数',202104 as '时间'

  union all

  select '主管级及以上员工' as '范围',3927 as '在职人数',63 as '离职人数',202104 as '时间'

  union all

  select '基层员工' as '范围',22080 as '在职人数',733 as '离职人数',202105 as '时间'

  union all

  select '主管级及以上员工' as '范围',3935 as '在职人数',71 as '离职人数',202105 as '时间'

  union all

  select '基层员工' as '范围',22021 as '在职人数',669 as '离职人数',202106 as '时间'

  union all

  select '主管级及以上员工' as '范围',3935 as '在职人数',71 as '离职人数',202106 as '时间'

  union all

  select '基层员工' as '范围',22039 as '在职人数',692 as '离职人数',202107 as '时间'

  union all

  select '主管级及以上员工' as '范围',3911 as '在职人数',47 as '离职人数',202107 as '时间'

  union all

  select '基层员工' as '范围',22061 as '在职人数',714 as '离职人数',202108 as '时间'

  union all

  select '主管级及以上员工' as '范围',3934 as '在职人数',70 as '离职人数',202108 as '时间'

  union all

  select '基层员工' as '范围',21904 as '在职人数',557 as '离职人数',202109 as '时间'

  union all

  select '主管级及以上员工' as '范围',3913 as '在职人数',49 as '离职人数',202109 as '时间'

  union all

  select '基层员工' as '范围',21905 as '在职人数',558 as '离职人数',202110 as '时间'

  union all

  select '主管级及以上员工' as '范围',3902 as '在职人数',38 as '离职人数',202110 as '时间'

  union all

  select '基层员工' as '范围',21915 as '在职人数',568 as '离职人数',202111 as '时间'

  union all

  select '主管级及以上员工' as '范围',3907 as '在职人数',43 as '离职人数',202111 as '时间'

  union all

  select '基层员工' as '范围',21929 as '在职人数',482 as '离职人数',202112 as '时间'

  union all

  select '主管级及以上员工' as '范围',3905 as '在职人数',41 as '离职人数',202112 as '时间'

  select * from #T

  declare @sql varchar(max)=''

  select @sql=@sql+

  ',max(case when a.时间='+cast(t.时间 as varchar(20))+' then a.在职人数 else NULL end) 在职人数 '+

  ',max(case when a.时间='+cast(t.时间 as varchar(20))+' then a.离职人数 else NULL end) 离职人数 '+

  ',max(case when a.时间='+cast(t.时间 as varchar(20))+' then a.时间 else NULL end) 时间 '

  from #T t group by t.时间

  set @sql='select a.范围'+ @sql +'from #T a group by a.范围'

  --set @sql='select * from #T'

  print(@sql)

  exec(@sql)

标签:示例,union,sqlserver,员工,行列,在职,人数,select,离职
来源: https://www.cnblogs.com/linjingyg/p/15727396.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有