首先引用一篇博文地址:http://www.cnblogs.com/luluping/archive/2009/07/24/1530611.html
主要是通过动态组合case语句来生成sql,并动态执行。
个人在此照着碎碎念一下:
首先:如何从一个数据集中组合生成多分支的case?
比如:select distinct subject from studentcj ,该语句执行结果返回'语文'、'数学','物理'等3条记录。
而我们要生成的case语句应该是这样的:
case subject when '语文' then result end,
--case when subject='语文' then result end
case subject when '数学' then result end,
case subject when '物理' then result end
--case when subject='语文' then result end
case subject when '数学' then result end,
case subject when '物理' then result end
其中,subject的值不确定就是这3种,数目不固定,因此,我们就需要动态的来构建这条sql语句了。
由于我们要从多条记录中生成一条语句,因此,我们要使用select,而不是set,
具体sql如下:
代码
declare @sql varchar(2000)
set @sql =''
select @sql=@sql+',case subject when '+sub.subject+' then result end' from (select distinct subject from studentcj) as sub
print @sql
--结果为:,case subject when 数学 then result end,case subject when 物理 then result end,case subject when 语文 then result end
set @sql =''
select @sql=@sql+',case subject when '+sub.subject+' then result end' from (select distinct subject from studentcj) as sub
print @sql
--结果为:,case subject when 数学 then result end,case subject when 物理 then result end,case subject when 语文 then result end
知道如何构建不确定数目的case后,接下来的工作就好办了。
------
总结一下行转列的办法:
--USE [MyTestDB]
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--SET ANSI_PADDING ON
--GO
--CREATE TABLE [dbo].[studentcj](
-- [uname] [varchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
-- [subject] [varchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
-- [result] [float] NOT NULL
--) ON [PRIMARY]
--
--GO
--SET ANSI_PADDING OFF
--truncate table studentcj
--insert into studentcj values('张三','语文',80)
--insert into studentcj values('张三','数学',90)
--insert into studentcj values('张三','物理',85)
--insert into studentcj values('李四','语文',85)
--insert into studentcj values('李四','数学',82)
--insert into studentcj values('李四','物理',96)
--法1
--select distinct c.uname as '姓名',
-- (select result from studentcj where uname=c.uname and subject='语文') as '语文' ,
-- (select result from studentcj where uname=c.uname and subject='数学') as '数学',
--(select result from studentcj where uname=c.uname and subject='物理') as '物理'
--from studentcj c
--法2
--select uname as '姓名',
--sum(case when subject='语文' then result end) as '语文',
--sum(case when subject='数学' then result end) as '数学',
--sum(case when subject='物理' then result end) as '物理'
--from studentcj group by uname
--法3
declare @sql varchar(2000)
set @sql='select uname as 姓名'
--下面这句要使用select,而不是set,因为它会使用一个数据集来组合条件
select @sql=@sql+',sum(case subject when '''+sub.subject+''' then result end) as '+sub.subject from (select distinct subject from studentcj) as sub
set @sql=@sql+' from studentcj group by uname'
--print(@sql)
exec(@sql)
--GO
--SET ANSI_NULLS ON
--GO
--SET QUOTED_IDENTIFIER ON
--GO
--SET ANSI_PADDING ON
--GO
--CREATE TABLE [dbo].[studentcj](
-- [uname] [varchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
-- [subject] [varchar](20) COLLATE Chinese_PRC_CI_AS NOT NULL,
-- [result] [float] NOT NULL
--) ON [PRIMARY]
--
--GO
--SET ANSI_PADDING OFF
--truncate table studentcj
--insert into studentcj values('张三','语文',80)
--insert into studentcj values('张三','数学',90)
--insert into studentcj values('张三','物理',85)
--insert into studentcj values('李四','语文',85)
--insert into studentcj values('李四','数学',82)
--insert into studentcj values('李四','物理',96)
--法1
--select distinct c.uname as '姓名',
-- (select result from studentcj where uname=c.uname and subject='语文') as '语文' ,
-- (select result from studentcj where uname=c.uname and subject='数学') as '数学',
--(select result from studentcj where uname=c.uname and subject='物理') as '物理'
--from studentcj c
--法2
--select uname as '姓名',
--sum(case when subject='语文' then result end) as '语文',
--sum(case when subject='数学' then result end) as '数学',
--sum(case when subject='物理' then result end) as '物理'
--from studentcj group by uname
--法3
declare @sql varchar(2000)
set @sql='select uname as 姓名'
--下面这句要使用select,而不是set,因为它会使用一个数据集来组合条件
select @sql=@sql+',sum(case subject when '''+sub.subject+''' then result end) as '+sub.subject from (select distinct subject from studentcj) as sub
set @sql=@sql+' from studentcj group by uname'
--print(@sql)
exec(@sql)
来源:https://www.cnblogs.com/McJeremy/archive/2010/03/18/1688722.html