select 姓名,
sum(case when 课程='语文' then 分数 else 0 end) 语文,
sum(case when 课程='数学' then 分数 else 0 end) 数学,
sum(case when 课程='物理' then 分数 else 0 end) 物理,
sum(case when 课程='英语' then 分数 else 0 end) 英语,
sum(case when 课程='德语' then 分数 else 0 end) 德语
from tb
group by 姓名
#2
你参考一下
SELECT P.* FROM [学生成绩表]PIVOT(MAX([分数])FOR[课程]IN([语文],[数学],[物理],[英语],[德语]))P
如果课程不固定,还可以用动态处理
#3
select a.* from tb pivot(sum(分数) for 课程 in(语文,数学,物理,英语,德语)) as a
DECLARE @SQL VARCHAR(8000)
SET @SQL='SELECT[姓名]'
SELECT @SQL=@SQL+',ISNULL(MAX(CASE[课程]WHEN '''+[课程]+'''THEN [分数]END ),60)['+[课程]+']'FROM(SELECT[课程]FROM[学生成绩表]GROUP BY[课程])C
SET @SQL=@SQL+'FROM[学生成绩表]GROUP BY[姓名]'
EXEC(@SQL)
#12
动态的话,你有课程表吗?记录有哪些课程
如果没有也可以~~
有课程表,但是课程有几百个,不能枚举到case吧,怎么解决呢
你试下,如#11动态处理
#13
-- 用临时表做的测试数据
WITH course(ID,课程) AS (
SELECT 1,'语文' UNION ALL
SELECT 2,'数学' UNION ALL
SELECT 3,'物理' UNION ALL
SELECT 4,'英语' UNION ALL
SELECT 5,'德语' UNION ALL
SELECT 6,'法语'
)
SELECT *
INTO #course
FROM course;
WITH student(姓名) AS (
SELECT '张三' UNION ALL
SELECT '李四' UNION ALL
SELECT '王五'
)
SELECT *
INTO #student
FROM student;
WITH tb (姓名,课程,分数) AS (
SELECT '张三','语文',74 UNION ALL
SELECT '张三','数学',83 UNION ALL
SELECT '张三','物理',93 UNION ALL
SELECT '张三','德语',null UNION ALL
SELECT '李四','语文',74 UNION ALL
SELECT '李四','数学',84 UNION ALL
SELECT '李四','物理',94 UNION ALL
SELECT '李四','英语',80
)
SELECT *
INTO #tb
FROM tb;
SELECT @columns = @columns + ', [' + [课程] + ']'
FROM #course
ORDER BY ID
SET @sql = '
SELECT *
FROM (
SELECT s.姓名,
c.课程,
ISNULL(t.分数,60) AS 分数
FROM #student s
JOIN #course c
ON 1=1
LEFT JOIN #tb t
ON t.姓名 = s.姓名
AND t.课程 = c.课程
) l
PIVOT (
Max (分数)
FOR 课程 IN ( ' + SubString(@columns, 3, Len(@columns)-2) + ')
) AS p'
--PRINT @sql
EXEC (@sql)
-- 用临时表做的测试数据
WITH course(ID,课程) AS (
SELECT 1,'语文' UNION ALL
SELECT 2,'数学' UNION ALL
SELECT 3,'物理' UNION ALL
SELECT 4,'英语' UNION ALL
SELECT 5,'德语' UNION ALL
SELECT 6,'法语'
)
SELECT *
INTO #course
FROM course;
WITH student(姓名) AS (
SELECT '张三' UNION ALL
SELECT '李四' UNION ALL
SELECT '王五'
)
SELECT *
INTO #student
FROM student;
WITH tb (姓名,课程,分数) AS (
SELECT '张三','语文',74 UNION ALL
SELECT '张三','数学',83 UNION ALL
SELECT '张三','物理',93 UNION ALL
SELECT '张三','德语',null UNION ALL
SELECT '李四','语文',74 UNION ALL
SELECT '李四','数学',84 UNION ALL
SELECT '李四','物理',94 UNION ALL
SELECT '李四','英语',80
)
SELECT *
INTO #tb
FROM tb;
SELECT @columns = @columns + ', [' + [课程] + ']'
FROM #course
ORDER BY ID
SET @sql = '
SELECT *
FROM (
SELECT s.姓名,
c.课程,
ISNULL(t.分数,60) AS 分数
FROM #student s
JOIN #course c
ON 1=1
LEFT JOIN #tb t
ON t.姓名 = s.姓名
AND t.课程 = c.课程
) l
PIVOT (
Max (分数)
FOR 课程 IN ( ' + SubString(@columns, 3, Len(@columns)-2) + ')
) AS p'
--PRINT @sql
EXEC (@sql)
select 姓名,
sum(case when 课程='语文' then 分数 else 0 end) 语文,
sum(case when 课程='数学' then 分数 else 0 end) 数学,
sum(case when 课程='物理' then 分数 else 0 end) 物理,
sum(case when 课程='英语' then 分数 else 0 end) 英语,
sum(case when 课程='德语' then 分数 else 0 end) 德语
from tb
group by 姓名
#2
你参考一下
SELECT P.* FROM [学生成绩表]PIVOT(MAX([分数])FOR[课程]IN([语文],[数学],[物理],[英语],[德语]))P
如果课程不固定,还可以用动态处理
#3
select a.* from tb pivot(sum(分数) for 课程 in(语文,数学,物理,英语,德语)) as a
#4
#5
你参考一下
SELECT P.* FROM [学生成绩表]PIVOT(MAX([分数])FOR[课程]IN([语文],[数学],[物理],[英语],[德语]))P
DECLARE @SQL VARCHAR(8000)
SET @SQL='SELECT[姓名]'
SELECT @SQL=@SQL+',ISNULL(MAX(CASE[课程]WHEN '''+[课程]+'''THEN [分数]END ),60)['+[课程]+']'FROM(SELECT[课程]FROM[学生成绩表]GROUP BY[课程])C
SET @SQL=@SQL+'FROM[学生成绩表]GROUP BY[姓名]'
EXEC(@SQL)
#12
动态的话,你有课程表吗?记录有哪些课程
如果没有也可以~~
有课程表,但是课程有几百个,不能枚举到case吧,怎么解决呢
你试下,如#11动态处理
#13
-- 用临时表做的测试数据
WITH course(ID,课程) AS (
SELECT 1,'语文' UNION ALL
SELECT 2,'数学' UNION ALL
SELECT 3,'物理' UNION ALL
SELECT 4,'英语' UNION ALL
SELECT 5,'德语' UNION ALL
SELECT 6,'法语'
)
SELECT *
INTO #course
FROM course;
WITH student(姓名) AS (
SELECT '张三' UNION ALL
SELECT '李四' UNION ALL
SELECT '王五'
)
SELECT *
INTO #student
FROM student;
WITH tb (姓名,课程,分数) AS (
SELECT '张三','语文',74 UNION ALL
SELECT '张三','数学',83 UNION ALL
SELECT '张三','物理',93 UNION ALL
SELECT '张三','德语',null UNION ALL
SELECT '李四','语文',74 UNION ALL
SELECT '李四','数学',84 UNION ALL
SELECT '李四','物理',94 UNION ALL
SELECT '李四','英语',80
)
SELECT *
INTO #tb
FROM tb;
SELECT @columns = @columns + ', [' + [课程] + ']'
FROM #course
ORDER BY ID
SET @sql = '
SELECT *
FROM (
SELECT s.姓名,
c.课程,
ISNULL(t.分数,60) AS 分数
FROM #student s
JOIN #course c
ON 1=1
LEFT JOIN #tb t
ON t.姓名 = s.姓名
AND t.课程 = c.课程
) l
PIVOT (
Max (分数)
FOR 课程 IN ( ' + SubString(@columns, 3, Len(@columns)-2) + ')
) AS p'
--PRINT @sql
EXEC (@sql)
-- 用临时表做的测试数据
WITH course(ID,课程) AS (
SELECT 1,'语文' UNION ALL
SELECT 2,'数学' UNION ALL
SELECT 3,'物理' UNION ALL
SELECT 4,'英语' UNION ALL
SELECT 5,'德语' UNION ALL
SELECT 6,'法语'
)
SELECT *
INTO #course
FROM course;
WITH student(姓名) AS (
SELECT '张三' UNION ALL
SELECT '李四' UNION ALL
SELECT '王五'
)
SELECT *
INTO #student
FROM student;
WITH tb (姓名,课程,分数) AS (
SELECT '张三','语文',74 UNION ALL
SELECT '张三','数学',83 UNION ALL
SELECT '张三','物理',93 UNION ALL
SELECT '张三','德语',null UNION ALL
SELECT '李四','语文',74 UNION ALL
SELECT '李四','数学',84 UNION ALL
SELECT '李四','物理',94 UNION ALL
SELECT '李四','英语',80
)
SELECT *
INTO #tb
FROM tb;
SELECT @columns = @columns + ', [' + [课程] + ']'
FROM #course
ORDER BY ID
SET @sql = '
SELECT *
FROM (
SELECT s.姓名,
c.课程,
ISNULL(t.分数,60) AS 分数
FROM #student s
JOIN #course c
ON 1=1
LEFT JOIN #tb t
ON t.姓名 = s.姓名
AND t.课程 = c.课程
) l
PIVOT (
Max (分数)
FOR 课程 IN ( ' + SubString(@columns, 3, Len(@columns)-2) + ')
) AS p'
--PRINT @sql
EXEC (@sql)