行转列,加where 条件的问题
行转列,加where 条件的问题。
建表:
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go
declare @where varchar(100)
declare @sql varchar(8000)
set @where='''数学'',''物理''';
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb where 课程 in('数学','物理') ) as a
set @sql = @sql + ' from tb group by 姓名'
print @where
exec(@sql)
declare @where varchar(100)
declare @sql varchar(8000)
set @where='''数学'',''物理''';
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb where 课程 in(@where) ) as a
set @sql = @sql + ' from tb group by 姓名'
print @where
print @sql
exec(@sql)
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go
declare @where table(课程 varchar(100))
declare @sql varchar(8000)
insert @where select '数学' union all select '物理';
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb where 课程 in(select * from @where) ) as a
set @sql = @sql + ' from tb group by 姓名'
print @sql
exec(@sql)
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
GO
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
go
declare @where varchar(100)
declare @sql varchar(8000)
set @where='''数学'',''物理''';
set @sql = 'select 姓名 '
select @sql = @sql + ' , max(case 课程 when ''' + 课程 + ''' then 分数 else 0 end) [' + 课程 + ']'
from (select distinct 课程 from tb where 课程 in(''+@where+'') ) as a
set @sql = @sql + ' from tb group by 姓名'
print @where
print @sql
exec(@sql)
--返回值是找到字符串的位置
SELECT @FindIndex = CHARINDEX(@Separator,@Text,@StartIndex)
--判断有没找到没找到返回
IF(@FindIndex =0 OR @FindIndex IS NULL)
BEGIN
--如果没有找到就表示找完了
SET @FindIndex = LEN(@Text)+len(@Separator)
END
--截取字符串函数SUBSTRING 第一个参数是要截取的字符串
-- 第二个参数是开始的位置
-- 第三个参数是截取的长度
SET @Content =SUBSTRING(@Text,@StartIndex,@FindIndex-@StartIndex)
--初始化下次查找的位置
SET @StartIndex = @FindIndex+len(@Separator)
--把找的的值插入到要返回的Table类型中
INSERT INTO @tempTable (Value) VALUES (@Content)
END
RETURN
END
go
DROP PROCEDURE set_课程
Go
CREATE PROCEDURE set_课程
@KC varchar(100)
AS
IF OBJECT_ID('temp_课程') IS NOT NULL DROP TABLE temp_课程;
create table temp_课程(课程 varchar(10));
insert into temp_课程 select distinct 课程 from tb where 课程 in (select value from dbo._split(@KC,','))
go
exec set_课程 '语文,物理'