• SQL server 动态行转列


    用聚合函数配合CASE语句实现行转列功能:

    现在分享一下具体实现代码:

    转换前效果:

    PlanName PlanType PlanLimit
    计划1 计划类型1 RMB 1,000,000
    计划1 计划类型2 RMB 1,000,000
    计划1 计划类型3 RMB 1,000,000
    计划2 计划类型1 RMB 1,000,000
    计划2 计划类型2 RMB 1,000,000
    计划2 计划类型3 RMB 1,000,000
    计划3 计划类型1 RMB 1,000,000
    计划3 计划类型2 RMB 1,000,000
    计划3 计划类型3 RMB 1,000,000

    1、静态实现行转列

     1 with main as
     2 (
     3     select '计划1' as PlanName,'计划类型1' as PlanType,'RMB 1,000,000' as PlanLimit
     4     union all
     5     select '计划1' as PlanName,'计划类型2' as PlanType,'RMB 1,000,000' as PlanLimit
     6     union all
     7     select '计划1' as PlanName,'计划类型3' as PlanType,'RMB 1,000,000' as PlanLimit
     8     union all
     9     select '计划2' as PlanName,'计划类型1' as PlanType,'RMB 1,000,000' as PlanLimit
    10     union all
    11     select '计划2' as PlanName,'计划类型2' as PlanType,'RMB 1,000,000' as PlanLimit
    12     union all
    13     select '计划2' as PlanName,'计划类型3' as PlanType,'RMB 1,000,000' as PlanLimit
    14     union all
    15     select '计划3' as PlanName,'计划类型1' as PlanType,'RMB 1,000,000' as PlanLimit
    16     union all
    17     select '计划3' as PlanName,'计划类型2' as PlanType,'RMB 1,000,000' as PlanLimit
    18     union all
    19     select '计划3' as PlanName,'计划类型3' as PlanType,'RMB 1,000,000' as PlanLimit
    20 )
    21 select PlanType as [计划]
    22     ,'计划1'=max(case PlanName when '计划1' then PlanLimit else null end)
    23     ,'计划2'=max(case PlanName when '计划2' then PlanLimit else null end)
    24     ,'计划3'=max(case PlanName when '计划3' then PlanLimit else null end)
    25 from main 
    26 where 1=1
    27 group by PlanType

    2、动态实现行转列

     1 -- =============================================
     2 -- Author:        <Anne>
     3 -- Create date: <2016/04/27>
     4 -- Description:    <查询SP_AnneTest表的数据>
     5 -- =============================================
     6 CREATE PROCEDURE [dbo].[SP_AnneTest]
     7 (
     8 )
     9 AS
    10     declare @sql varchar(8000)
    11 BEGIN
    12     set @sql=''
    13 
    14     select @sql=@sql+','+''''+[PlanName]+''''+'=max(case PlanName when '''+[PlanName]+''' then PlanLimit else null end)'
    15     from main
    16     where 1=1
    17     group by PlanType
    18     set @sql='select PlanType as [''计划'']'+@sql+' 
    19             from main 
    20             where 1=1
    21             group by PlanType'
    22     
    23     print @sql
    24     exec(@sql)
    25 
    26 END
    27 GO

    实现效果:

    计划 计划1 计划2 计划3
    计划类型1 RMB 1,000,000 RMB 1,000,000 RMB 1,000,000
    计划类型2 RMB 1,000,000 RMB 1,000,000 RMB 1,000,000
    计划类型3 RMB 1,000,000 RMB 1,000,000 RMB 1,000,000
  • 相关阅读:
    HISI MPP模块 AO 和 ADEC 参数配置(转)
    HISI H264/H265 P帧帧内刷新功能,降低I帧太大带来的网络冲击(转)
    LiteOS系统--快速烧写.bin文件到norflash相应位置(转)
    海思平台(hi3559av100)的pq_sample代码编译过程和执行过程(转)
    MySQL主从同步配置
    WebLogic
    为什么公司用weblogic而不用tomcat?
    查看和更改rpm的默认安装路径的方法
    VM options 以及 properties文件的一些理解
    Could not transfer artifact xxx from/to xxx解决方案
  • 原文地址:https://www.cnblogs.com/AnneHan/p/5541961.html
Copyright © 2020-2023  润新知